3
function def() {
    console.log(this.x)
}

var f = def.bind({ x:777 })
f() // prints 777

The bind creates a function f which is identical to def, except that within f, this is set to { x:777 }.

Is it possible to access the object f was bound to outside of f? E.g., console.log(f.this.x) (but that doesn't work). Or is it impossible for code that comes after to see what object f was bound to?

8
  • how can u call a f().. its not yet declared as funciton.. Commented Mar 8, 2013 at 6:25
  • bind changes the context of this inside a function but it doesn't execute it like call and apply. I don't see what's the point of your code. x won't be accessible I don't think so. Commented Mar 8, 2013 at 6:26
  • 1
    So you're basically asking how to get the bound value of x from the this argument? Commented Mar 8, 2013 at 6:26
  • 1
    I've expanded your question a bit, as I think you were getting some downvotes because people didn't understand what you were asking. I hope you don't mind. Commented Mar 8, 2013 at 6:41
  • 1
    I have to disagree with the close vote. This is a perfectly legitimate question, which I would also like to know the answer to. He wants to know, given a function bound to an object using bind, is it possible to extract the bound object from the function Commented Mar 8, 2013 at 6:47

2 Answers 2

8

I found some useful information on bind here: http://dmitrysoshnikov.com/notes/note-1-ecmascript-bound-functions/

bind as specified in ECMAScript 5 produces a sort of lightweight function (which differs in some ways from usual functions, as described in the link above. Basically it provides a wrapper for calling the target function, and maintains internal properties which include the target function, the bound this, and the bound arguments. As these are internal properties, they aren't accessible in the way the OP is asking about (you can't take an arbitrary bound function f and do something like f.getBoundThis()).

It's worth noting that bind is not unique in capturing some state. Closures also capture state. However, bind (as specified in ECMAScript 5) is not a closure, because closures capture variables whereas bind captures values.

Here's an example:

(function () {
    var x = 2;

    function thisSquared() { return this * this; }
    f = thisSquared.bind(x);

    g = function() { return x * x; } // g is a closure

    console.log(f()); // Squares the captured value (2), prints 4
    console.log(g()); // Squares x, prints 4

    x = 3;
})();

console.log(f()); // Squares the captured value (still 2), prints 4
console.log(g()); // Squares x, prints 9

Some previous implementations of bind (written in JavaScript before ECMAScript 5) didn't have this distinction from closures.

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

Comments

0

No, you cannot access it because the object is only bound temporarily to the function for the lifetime of the call, it does not change the functions prototype.

11 Comments

But what if the binding happens within some function, and then the bound function is returned? Then you only have the function, but the bound object still exists. The question is if you can get the bound object from the function it's bound to.
Surely this will print 777, but the question is can you access the bound object if you only have a reference to a function instance returned by bind().
The short answer to both of your comments is "yes". Of course it depends on the implementation, but if you can either: 1) ensure all bound functions return this or 2) assign this to a variable bound to an outer function (not really ideal). bind, call, and apply all return the value the function they are executing returns.
Rob M. I am not sure I understand your answer. Do you mean that it is possible to access the bound object somehow by using apply() or call()?
@RobM., I'm not sure you're understanding. If I have a function f which was created using bind as in exebook's example, how do I extract the object that was passed to the bind? Obviously if I stored the object in some variable, or if f returns the object, then I can see it. But in the general case, is there some way to read the object given f. E.g., I could imagine an f.this, but it doesn't exist.
|

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.