0

Lets take an object

var o = {a : 10 ,b:20,f:function(){ return this.a + this.b; }};
console.log(o.f())//30 works as expected

Now,

var o = {a:10,b:20,f:function(){ return this.a + this.b; }};
var p = o;
p.a = 1;
p.b = 4;
console.log(p.f()); // 5 works fine
console.log(o.f()); //5,it should 30 right but why it is showing 5

why is it working like this. If i do o.f() it should get the value from object o.

It looks like i have not understood the bind properly

console.log(o.f.bind(o)())//gives 5
console.log(o.f.bind(this)())//gives 30

please give the difference between these two lines codes.

4
  • values in o changed already . Commented Sep 19, 2016 at 5:12
  • console.log(o.f.bind(this)) , what is this ? Commented Sep 19, 2016 at 5:13
  • 1
    I get NaN on o.f.bind(this)() Commented Sep 19, 2016 at 5:17
  • it look like in console i had a window.a and window.b Commented Sep 19, 2016 at 5:22

3 Answers 3

3

You are assigning an object reference to another one variable, not a copy.

var o = {a:10,b:20,f:function(){ return this.a + this.b; }};
var p = o; //Assigning the reference of o to p.

Hence it is behaving like that and this is how the language works.

When comes to your second question,

console.log(o.f.bind(o)()); // 5 

This doesn't have any effect at all. You are not at all changing the this value of that function.

But the second line of code,

console.log(o.f.bind(this)()); // NaN

Changes the this value of that function to the this value of the context where the bind is being executed. For example, If you are executing it from window's context, this would be window, window.a will be undefined and window.b will also be undefined. So undefined + undefined will return NaN. Not 30 as you are saying.

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

2 Comments

console.log() both the variables after assignment
OMG i forgot completely, what about the second question about bind
2

In the second block of code, o and p are the same object.

That is, p.a and o.a refer to the same value:

var o = {x: 5, f: function() {return this.x;}};
var p = o;
o.x = 10;
console.log(p.x) // 10

The results of your second block of code aren't related to this or bind.


.bind(newObject) makes a new function with the value of this "changed" to the specified newObject. (Aside: It usually doesn't make sense to use this outside of a method, because it will be defined as window [which also holds all global variables].)

For example, consider these objects:

var u = {x: 5, f: function() {return this.x;}};
var v = {x: 20};

If you want to "copy" the method from u to v, you could do this:

v.f = u.f.bind(v);

console.log(u.f()) // 5 // .bind() doesn't change the old function
console.log(v.f()) // 20

Comments

1

p = o copies a reference, not creates a new object. Using a constructor function (with f() in prototype) or closure could be what you are looking for.

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.