Royi Namir
Royi Namir

Reputation: 148524

Javascript and [bind] implementation?

I read a bit about the bind implementation in JS ( from John Resig's book ) :

It looks something like this : ( jsbin)

#1Function.prototype.bind = function ()
#2{
#3    var fn = this,           
#4            args = Array.prototype.slice.call(arguments),
#5            object = args.shift();
#6
#7        return function ()
#8        {                    
#9            return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));  
#10       };
#11   };

And I can test it like this:

var myObject = {};

function myFunction()
{
    return this == myObject;
}


//--------- 
if (!myFunction()) alert("Context is not set yet"); //not set ye

var bindedFunction= myFunction.bind(myObject)

if (bindedFunction()) alert( "Context is set properly"); //set properly
//--------- 

However I have 2 question about the it :

Question #1

In Line #3 , the this is referes to the function which is executed , correct ? but isn't this suppose to be : "the object which holds the function" ?

If I do alert(this) inside the bind function , I see this :

function myFunction()
{
    return this == myObject;
}

Question #2

In line #9 , Why does the returned function is

  return function ()
        {                      
            return fn.apply ...
        };

and not

  return function ()
        {                      
           fn.apply ...
        };

I dont understand it :

the returned function creates a closure and should return a function which is only executing fn.apply(o....

I don't understand why it returns additional return : like in return fn.apply(o....

Upvotes: 2

Views: 607

Answers (3)

Alnitak
Alnitak

Reputation: 339786

In the context (no pun intended) of Function.bind, this does indeed refer to the currently invoked function, not the "object" that might contain it as a method.

Remembering that functions are first class objects in JS, when you write

myFunction.bind()

then myFunction is the object and .bind is then a method of that object (inherited via Function.prototype), and then when you invoke it the normal rules apply - this === myFunction.

For q.2, the result of fn.apply is required so that the bound function can also return a result, just as the original function might have.

Upvotes: 2

Ja͢ck
Ja͢ck

Reputation: 173532

but isn't this suppose to be : "the object which holds the function" ?

No. This is because internally fn.bind(obj) is invoked like this:

Function.bind.call(fn, window, obj)

Therefore, this refers to fn.

I don't understand why it returns additional return : like in return fn.apply(o....

The function .bind() is meant to return a function that, when invoked, "proxies" the return value from your given function, i.e. myFunction. If not, it would always return undefined.

Upvotes: 1

Barmar
Barmar

Reputation: 780724

The answer to #2 is that the bound function should return whatever the original function returns, so it needs a return statement.

Upvotes: 0

Related Questions