javascript - 无法读取 JSON 中 parent 的属性(property)

标签 javascript jquery json

为什么它不起作用?

我收到此错误消息:

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/

相关文章:

javascript - 使用RegExp动态创建正则表达式,并过滤内容

java - 如何为此 REST 响应编写 getter setter 类以进行改造

javascript - 如何通过 Ajax 和 PHP 使用数据切换

javascript - x || 之间的任何区别[] 和 x ?? [] 在 JavaScript 中?

javascript - 带有按钮隐藏和显示的数据表在 angularjs 代码中

javascript - 使用 javascript 将 JSON 对象转换为 XML

javascript - 如何修改与fineuploader中的上传请求相关的响应的正文内容?

javascript - Rails 表单和对不同 Controller 的自定义 Ajax 调用

jquery - 在 div 中向下移动文本输入

jquery - 使用引导开关切换禁用输入框