When creating objects with member values of objects, why is the 'this' keyword needed to call other members within the same scope? In the following code, for example, this.name had to be called from function(). Why can function() here not be able to access name without using 'this'? My understanding is that a function is executed when it is called and the value (Brian) at the time of execution would theoretically be already declared and available...
Why does this work:
var person2 = {
name: 'Brian',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
but not this:
var person2 = {
name: 'Brian',
greeting: function() {
alert('Hi! I\'m ' + name + '.');
}
}
Aucun commentaire:
Enregistrer un commentaire