3

The Facts

Function('return this')() always returns the global (window) object. Function.bind({})('return this')() returns the global object too.

My Goals

I want to create a variation of Function. The anonymous functions returned by calling that variation of Function should always use myObj as this.

If JavaScript wouldn't behave in that special way (see The Facts), I would do the following:

var myFun = Function.bind(myObj);

myFun is the object that I want to own. Now I would be able to do the following:

console.assert(myObj === myFun('return this')());

My Questions

  • Why is Function returning global, even after .bind()ing it to another object?
  • Is there a workaround? How can I bind Function to another object?

Thanks.

2 Answers 2

1

You are essentially doing this:

Function.call({}, 'return this;')();

The Function function is executed in the context of a new anonymous object. Doing this does not affect the context of the functions generated by Function. It turns out that Function doesn't care what context it runs in -- it always produces functions that have the default global context.

If you want to specify the context of the functions generated by Function, you want to wrap Function like this:

// give our vars privacy in a closure
(function() {
    // store old Function
    var oldFunc = Function;

    // redefine Function to be a wrapper around the real Function
    // which takes an additional `context` argument
    Function = function(ftext, context) {
        return oldFunc(ftext).bind(context);
    }
}());

Now you can call Function('return this', myObj)(); and it will return myObj.


Or, to simply create your suggested myFun(text) syntax which passes your assert test:

var myFun = function(ftext) {
    return Function(ftext).bind(myObj);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know what exactly you are trying to achieve, but it seems that your method chaining is in the wrong order.

Function('return this').bind({})() // returns the empty Object

6 Comments

Thanks, that's I nice in this use case. - But I want to create a variation of Function itself, not a variation of a function created by Function. I want something like var myFun = Function.bind(myObj) as a result in a variable. If I was using your solution, I can't do something like that - Then I'd need to .bind() every produced by Function apart.
Hm, either I am not aware enough of JavaScript or I don't understand the basic principle behind your idea. But it sounds to me that you want to overload the prototype of .bind. Sorry if I wasn't any of help.
I extended my question in order to make clear what I mean. I hope it's OK now.
"it sounds to me that you want to overload the prototype of .bind": That's not really what I keep in mind. - I don't want to do anything with .bind(), bind() is just my tool to create a second version of Function. I want that second version of Function (var myFun = Function.bind(myObj);) to return special anonymous functions (var myRetValue = myFun('return this')) when getting called. The special thing concerning these anonymous functions should be: In the scope of these returned functions, this should refer to myObj (console.assert(myObj === myRetValue());).
So it's all about changing the context of the anonymous functions returned by a Function() call. - Is it clear what I want to express? I hope so. If it's not: Please tell me what's wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.