【发布时间】:2014-11-18 15:17:49
【问题描述】:
我正在尝试向我的对象文字添加元素,我收到的只是控制台中的undefined 输出。
为什么 itemSize 被打印为undefined 以及为什么我试图设置的所有属性也未定义。我一定在这里遗漏了一些重要的东西。
我也不明白get testFunction 的用途以及它是如何被访问的,因为它似乎没有遵循函数名称后有冒号的其他函数的签名。
我现在很困惑。
index.html
var square = new GULE.Scene();
square.hey( 5 );
gule.js
var GULE = GULE || {};
GULE.Scene = function ( itemSize ) {
this.itemSize = itemSize;
console.log( "Trying in constructor: " + this.itemSize );
console.log( "Passed variable: " + itemSize );
};
GULE.Scene.prototype = {
constructor: GULE.Scene,
stuff: 1,
get testFunction () {
console.log( "testFunction!" );
},
add: function () {
this.stuff += 1;
},
hey: function () {
console.log( this.stuff );
console.log( "Trying in function: " + this.itemSize );
}
};
【问题讨论】:
-
因为你没有向函数传递任何值。应该是 new GULE.Scene(
); -
天哪。那么现在这个错误已经解决了,
get testFunction的目的是什么,我不明白它是如何工作的。