function Person(name, age, weight) {
this._name = name;
this._weight = weight;
this._age = age;
}
Person.prototype = {
Anatomy: {
Weight: this._weight,
Height: (function() {
//calculate height from age and weight
})
}
}
我預計Anatomy.weight
爲60,當我跑這個代碼代碼:
令我驚訝它是未定義的。在檢查時,似乎this
指的是全局對象窗口。現在什麼這裏發生了:( 我預計this._weight
指人從粗略計算物品重量,否則,這應該有被稱爲解剖最少,因爲它是一個對象。可能有人澄清疑問
顯然明白,不能從我上面所解釋的完成。那麼應該怎麼做?這不是最好的回答被投票 – Deeptechtons 2011-05-27 06:23:24
@deeptechtons,我編輯答案提供一個替代。看到這裏還有一個工作jsfiddle:http://jsfiddle.net/C3puH/ – 2011-05-27 06:29:21
你贏得了比賽,感謝那整潔的代碼 – Deeptechtons 2011-05-27 06:44:11