2
我有以下代码:为什么吸气返回旧值
function User(fullName) {
this.fullName = fullName;
Object.defineProperties(this,
{
firstName: {
get: function() {
return fullName.split(" ")[0];
}
,
set: function (fName) {
this.fullName = fName + " " + this.lastName;
}
},
lastName: {
get: function() {
return fullName.split(" ")[1];
}
,
set: function (lName) {
this.fullName = this.firstName + " " + lName;
}
}
})
}
和下面的代码来执行:
var vasya = new User("oldName oldSurname");
console.log(vasya.firstName); //
vasya.firstName = "newName";
vasya.lastName = "newSurname"
console.log(vasya.fullName);
这个输出newName OldSurname
如果改变了一点:
var vasya = new User("oldName oldSurname");
console.log(vasya.firstName); //
console.log(vasya.lastName); //
vasya.firstName = "newName";
vasya.lastName = "newSurname"
console.log(vasya.fullName);
返回oldName newSurname
请解释为什么现在我看到oldName
insted的的newName