fridojet
fridojet

Reputation: 1296

JavaScript: How To `.bind()` `Function` to Another Object

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

Thanks.

Upvotes: 3

Views: 1797

Answers (2)

apsillers
apsillers

Reputation: 115950

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);
}

Upvotes: 1

dan-lee
dan-lee

Reputation: 14492

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

Upvotes: 0

Related Questions