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