JavaScript function binding (this keyword) is lost after assignment
Posted
by Ding
on Stack Overflow
See other posts from Stack Overflow
or by Ding
Published on 2010-04-23T21:07:54Z
Indexed on
2010/04/23
21:13 UTC
Read the original article
Hit count: 353
JavaScript
this is one of most mystery feature in JavaScript, after assigning the object method to other variable, the binding (this keyword) is lost
var john = {
name: 'John',
greet: function(person) {
alert("Hi " + person + ", my name is " + this.name);
}
};
john.greet("Mark"); // Hi Mark, my name is John
var fx = john.greet;
fx("Mark"); // Hi Mark, my name is
my question is:
1) what is happening behind the assignment? var fx = john.greet; is this copy by value or copy by reference? fx and john.greet point to two diferent function, right?
2) since fx is a global method, the scope chain contains only global object. what is the value of this property in Variable object?
© Stack Overflow or respective owner