2

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

1
  • Q2: because some bound functions may return something and you want to get it after you call it. So that return returns what myFunction returns Commented Apr 11, 2013 at 10:59

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

what about the question but isn't this suppose to be : "the object which holds the function" ?
@RoyiNamir it's still true. The object that contains the bind method is the function, because it's part of Function.prototype.
@RoyiNamir yes, because functions are first-class objects in JS.
Everything is an object in JavaScript. "hello".toLowerCase(), 5.4.isNaN(), (true).valueOf() and so on.
@Alnitak so why this is working without the return jsbin.com/osanun/3/edit ?
|
1

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.

Comments

0

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

Comments

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.