function student () {
var name = "xxx";
var getName = function () {
return name
}
var setName = function (newName) {
name = newName;
}
return {
getName: getName,
setName: setName,
name: name
}
}
var studentA = student();
console.log(studentA.getName())
studentA.setName("aaa");
console.log(studentA.getName())
console.log(studentA.name);
Why is the output
doesn"t it make sense that the name has become aaa?
< hr >more instructive is to add this.name to both get and set. May I ask why the two name I wrote before are not in the same scope?
< hr >I see. I confuse the space allocated by the closure with the space of return, so it is useless that this is a modification to the space of the closure, and then using this is a modification to the object of this return