-1

All:

I wonder if I want to bind this to an object function, how can I do that? Like:

var a  = {
    name:"nihao", 
    hello: (function(){
        console.log(this.name);
    }).bind(this)
}

When I run a.hello(), the this is Window. Then I change the code to :

var a  = {
    name:"nihao", 
    hello: (function(){
        console.log(this.name);
    }).bind(a)
}

Nothing changes, so I wonder how can I bind a as this when I init this object?

Thanks

Kuan
  • 10,085
  • 18
  • 76
  • 168
  • @HunanRostomyan Thanks, you mean in both situations? I tried, but neither works – Kuan Nov 23 '15 at 23:12
  • Remove the bind inside the definition and bind it during the call. For example, `a.hello.call({name: 'Kuan'})` will print "Kuan". – Hunan Rostomyan Nov 23 '15 at 23:13
  • Needless to say, if you just call `a.hello.call(a)`, you'll get "nihao". – Hunan Rostomyan Nov 23 '15 at 23:14
  • Just don't bind your function at all, then the result of `a.hello()` will be the expected one :-) And apart from that, if you have a singleton here [you could just as well use `a.name` inside the function](http://stackoverflow.com/q/10711064/1048572). – Bergi Nov 23 '15 at 23:19
  • @Bergi Thanks, the reason I use this is: I am trying to build a factory function which can clone object from a. BTW, I still have not figured out how to clone a object, could you give some clues for that? – Kuan Nov 23 '15 at 23:39
  • @Kuan Not sure what the factory has to do with this. Regardless, you'd clone our objects a lot easier if you didn't bind their methods. – Bergi Nov 24 '15 at 00:01
  • @Bergi Thanks, just forget my code, what would you do if you need to clone a object? – Kuan Nov 24 '15 at 00:16
  • @Kuan: Depends a lot on the needs. Probably something along [these lines](http://stackoverflow.com/q/25553910/1048572). – Bergi Nov 24 '15 at 00:19

3 Answers3

1

In this instance you need to bind the function later-on; once "a" has been initialized.

a.hello = (function hello(){}).bind(a);
Ben Aston
  • 45,997
  • 54
  • 176
  • 303
  • thanks, so this means I can only do this binding after init, right? – Kuan Nov 23 '15 at 23:18
  • If you want to bind to "a", in this case yes. – Ben Aston Nov 23 '15 at 23:19
  • Ok. Thanks. Could you give me a little detail why bind in init not work( like how JS engine treat this) – Kuan Nov 23 '15 at 23:21
  • bind does work during "init", but you are trying to bind to "a" which is undefined at the point of attempted binding. As Bergi points out, you probably don't need to bind anything. But if you do, my answer stands. – Ben Aston Nov 23 '15 at 23:24
  • Thanks, the reason I use this is: I am trying to build a factory function which can clone object from a. BTW, I still have not figured out how to clone a object, could you give some clues for that? – Kuan Nov 23 '15 at 23:39
  • Does the object to be cloned have functions on it? What you probably want is to create an object containing the methods, and for this to become the prototype. The data properties can be added direct to the "clone". – Ben Aston Nov 23 '15 at 23:41
  • Yes, it does. But I do not know how to clone all the functions bind to that object's prototype. Could you show a little code snippet of that? – Kuan Nov 23 '15 at 23:55
1

First, don't add the bind inside the object definition:

var a  = {
  name: "nihao", 
  hello: function(){
    console.log(this.name);
  }
}

Then, during the call, pass whatever object you want to be this:

a.hello.call(a);
// => "nihao"

a.hello.call({name: "Kuan"});
// => "Kuan"
Hunan Rostomyan
  • 2,072
  • 2
  • 19
  • 29
0

Why not?

var a  = {
    name:"nihao", 
    hello: function(){
        console.log(this.name);
    }
}

a.hello();

By default, the function property inside an object is bound to that instance.

DJ.
  • 6,212
  • 1
  • 28
  • 44