“属性描述必须是对象". JavaScript中的错误.不明白为什么:( [英] "Property description must be an object" error in javascript. Can't understand why :(

查看:436
本文介绍了“属性描述必须是对象". JavaScript中的错误.不明白为什么:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码,我会遇到以下问题:

Using this code I have this issue:

    $.fn.dxwShow = function (options) {

    console.log(typeof(options));
    dxwShowSetOptions(options);

    setInterval(function(){
        dxwShowChange();
    }, dxwShowOptions.time);
};

var dxwShowOptions = {
    "transition" : "SlideToggle",
    "time": 1000
};

var dxwShowStatus = {
    current : 0
};

function dxwShowSetOptions(options){
    console.dir(typeof(options));

    dxwShowOptions = Object.create(dxwShowOptions, options);
}

function dxwShowChange(){
    console.log(dxwShowOptions);
};

$(function(){

    options = {
        "time": 700,
        "debug" : true
    };

    $("#dxwShow").dxwShow(options);

});

我想更新dxwShowOptions,所以我使用Object.create首先传递要复制的对象,然后传递包含新参数的对象.错误在哪里?

I want to updtate dxwShowOptions and so i use Object.create passing first the object i wanna copy and so the object containing the new parameters. Where is the mistake?

PS:Chrome说该对象位于Object.create行.

PS :Chrome say that the object is at the Object.create line.

推荐答案

Object.create带有属性描述符的映射. options不是这样的列表.

Object.create takes a map of property descriptors. options is not such a list.

请参见 https://developer.mozilla.org /en-US/docs/JavaScript/Reference/Global_Objects/Object/create

如果您仍然想使用Object.create,则需要将选项修改为更像

If you wanted to still use Object.create, you'd need to modify options to be something more like

var options = {
    time: {
        enumerable: true,
        configurable: true,
        writable: true,
        value: 700
    },
    debug: {
        enumerable: true,
        configurable: true,
        writable: true,
        value: true
    }
};

但是您可能想使用更多类似 _.extend 的东西.

But probably you want to use something more like _.extend.

这篇关于“属性描述必须是对象". JavaScript中的错误.不明白为什么:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆