Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • 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.

like image 865
fridojet Avatar asked Jan 25 '26 12:01

fridojet


1 Answers

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);
}
like image 190
apsillers Avatar answered Jan 28 '26 03:01

apsillers