的JavaScript學習:給這個對象:在財產申報中不能做數學運算嗎?這裏
var ivan = {
name: 'Ivan',
yearOfBirth: 1973,
age: 2017 - this.yearOfBirth
}
console.log(ivan.age);
爲什麼說的console.log NaN
?我不能在財產申報中進行簡單的數學運算嗎?如果不是,爲什麼?
的JavaScript學習:給這個對象:在財產申報中不能做數學運算嗎?這裏
var ivan = {
name: 'Ivan',
yearOfBirth: 1973,
age: 2017 - this.yearOfBirth
}
console.log(ivan.age);
爲什麼說的console.log NaN
?我不能在財產申報中進行簡單的數學運算嗎?如果不是,爲什麼?
因爲this.yearOfBirth
碰巧還沒有定義,你可以使用匿名函數作爲構造:
var ivan = new function() {
this.name = 'Ivan',
this.yearOfBirth = 1973,
this.age = 2017 - this.yearOfBirth
};
希望這有助於。
var ivan = new function() {
this.name = 'Ivan',
this.yearOfBirth = 1973,
this.age = 2017 - this.yearOfBirth
};
console.log(ivan.age);
'this.yearOfBirth'沒有定義尚未... –
'this'並不是指你在製作不同類型的處理對象。 – Quentin
您可以在這個問題上找到解決方案的答案https://stackoverflow.com/questions/2787245/how-can-a-javascript-object-refer-to-values-in-it-本身 –