-2
私は誤ってこのキーワードをsetter、getterメソッドの中で紛失しています。それはいくつかの奇妙なバグにつながる:(クローム、Firefoxでテストした)javascriptのsetter、getterメソッドに "this"というキーワードがありません
ケース1:
let user = {
name: "John",
set fullName(value) {
name = value;
},
get fullName() {
return name;
}
};
user.fullName // ""
user.fullName = "Batman"
user.fullName // "Batman"
user.name // "John"
なぜまだ "ジョン" プロパティ名のですか? 「バットマン」はどこから来たのですか?
ケース2:上記のコードの変数名を変更し、何かが起こる:
let user = {
anythingButName: "John",
set fullName(value) {
anythingButName = value;
},
get fullName() {
return anythingButName;
}
user.fullName // anythingButName is not defined at Object.get fullName [as fullName]...
};
上記のコード内の変数には任意の名前が、単語名を使用することはできません。どうしてか分かりません?
console.log(window.name); –