为什么它不起作用?
我收到此错误消息:
TypeError: Cannot read property 'width' of undefined
var option = jQuery.extend({
parent: this,
width: 280,
height: 280,
circle: {
x: (option.width / 2) + 5, // <= HERE!
y: (option.height / 2) + 22, // <= AND ALSO HERE!
radius: 70,
speed: 5,
rotation: 0,
angleStart: 270,
angleEnd: 90,
hue: 220,
thickness: 15,
blur: 20
}
}, options);
如何读取“parent”属性?
我应该使用另一个前缀吗?
最佳答案
你不能像那样处理对象,你必须做这样的事情;
var width = height = 280; // store values
var option = jQuery.extend({
parent: this,
width: width,
height: height
circle: {
x: (width / 2) + 5, // option doesn't exist
y: (height / 2) + 22, // option doesn't exist
radius: 70,
speed: 5,
rotation: 0,
angleStart: 270,
angleEnd: 90,
hue: 220,
thickness: 15,
blur: 20
}
}, options);
// option is now created and exists here
你可以这样做
var tempOption = {
parent: this,
width: 280,
height: 280
};
tempOption.circle = {
x: (tempOption.width / 2) + 5, // option doesn't exist
y: (tempOption.height / 2) + 22, // option doesn't exist
speed: 5,
rotation: 0,
angleStart: 270,
angleEnd: 90,
hue: 220,
thickness: 15,
blur: 20
};
var option = jQuery.extend(tempOption, options);
关于javascript - 无法读取 JSON 中 parent 的属性(property),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21123442/