【发布时间】:2013-09-11 17:01:07
【问题描述】:
我为我的 js 使用对象字面量,在下面你可以看到它是“全局”变量。其中之一是一个对象(theBody),它又包含一个称为“body”的数组。该数组包含多个对象(以下示例中只有一个),它们是 svg 对象。
我希望能够从一个名为 bodyColor 的特定变量分配填充值,但是当我更改时:
'fill':'#e59225',
到
'fill': AvGen.theBody.bodyColor,
我收到错误 Uncaught ReferenceError: theBody is not defined
为什么会这样?如何访问对象属性的 bodyColor?
来自 js:
var AvGen = {
paper: null,
theBody: {
bodies: [
[0,0,277.9,308.5,{
type:'path',
'fill':'#e59225',
'stroke':'none',
'stroke-width':'0',
'fill-opacity':'1',
'stroke-opacity':'0'
}],
],
currNr: 1,
currObj: null,
bodyColor: '#e59225'
},
init: function() {
}
}
【问题讨论】:
-
test
AvGen是否被定义,只需做一个console.log 来测试它,因为错误明确指定Uncaught ReferenceError: AvGen is not defined -
我猜
AvGen是在您尝试使用它之后定义的,或者它是在闭包中定义的并且您引用它的代码不在范围内。
标签: jquery object properties object-literal