在这段代码中,当我更改格式时,它会在 Set 函数中的 else 语句中抛出该错误?

Where in this code that when I change the format it will throw that error in the else statement within the Set Function?

教程说如果全名的格式不正确,即名字、space 和姓氏,它将引发错误。

let person = {
firstName: 'John',
lastName: 'Doe'
}



Object.defineProperty(person, 'fullName', {
get: function () {
    return this.firstName + ' ' + this.lastName;
},
set: function (value) {
    let parts = value.split(' ');
    if (parts.length == 2) {
        this.firstName = parts[0];
        this.lastName = parts[1];
    } else {
        throw 'Invalid name format';
    }
}


console.log(person.fullName);

此代码将显示在控制台中:

John Doe 

我真的很想理解代码,但是 set 函数如何在 else 语句中抛出这些 'Invalid name format'?

呃,在这儿吗?

else {
 throw 'Invalid name format';
}

我不太明白,这是你要的吗?