我創建了一個「人」的構造:如何創建其值根據其他變量而變化的變量?
function Person(firstName, secondName) {
'use strict';
this.firstName = firstName;
this.secondName = secondName;
this.fullName = this.firstName + ' ' + this.secondName;
}
,然後創建它的一個實例:
var person1 = new Person('David', 'Johns');
的person1.fullName
值現在將David Johns
我試圖修改的person1.firstName
值into George
我預計person1.fullName
的值更改爲George Johns
但它沒有c上吊!
那麼如何製作一個屬性取決於其他屬性的對象呢? 或其值取決於其他變量的變量?
您使用的屬性設置最終的答案我們。 *我可以嗎?不,沒有真正的理由來優化/記憶像這樣的計算屬性。 –