嗨,我的代码如下所示:
var myClass = {
globalVar : {
total : 100
},
myFunction : {
getTotal : function() {
return this.globalVar.total;
}
},
};
// Uncaught TypeError: Cannot read property 'total' of undefined
alert(myClass.myFunction.getTotal() );
关键字this
返回undefined,这是为什么呢?是因为我使用var myClass
而不是function myClass()
?
谢谢
[编辑] 这是 JSFiddle http://jsfiddle.net/DarcFiddle/cg7Fk/