function Person(name) {
Object.defineProperty(this, "name", {
get: function () {
return name;
},
set: function (newName) {
name = newName;
},
enumerable: true,
configurable: true
});
this.sayName = function () {
console.log(this.name);
};
}
var person1 = new Person("Nicholas");
var person2 = new Person("Greg");
console.log(person1.name); // Nicholas
console.log(person2.name); // Greg
person1.sayName(); // Outputs "Nicholas"
person2.sayName(); // Outputs "Greg"
How is that the accessor property name can use the named parameter name to store its value? Isn't a local variable garbage-collected after the execution of its containing context (in this case, the Person constructor)?