Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Property description must be an object" error

Tags:

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);
});

I want to update 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 say that the object is at the Object.create line.

like image 962
DxW Avatar asked Aug 20 '12 04:08

DxW


People also ask

What does object property mean?

A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects. The properties of an object define the characteristics of the object.

How do you assign a property to an object?

To add a new property to a Javascript object, define the object name followed by the dot, the name of a new property, an equals sign and the value for the new property.

What are property descriptors in JavaScript?

A property descriptor is a simple JavaScript object associated with each property of the object that contains information about that property such as its value and other meta-data. In the above example, we have created a plain JavaScript object myObj using literal syntax with myPropOne and myPropTwo properties.

What is object create in JavaScript?

The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.


1 Answers

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

See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/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
    }
};

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

like image 185
Domenic Avatar answered Nov 09 '22 11:11

Domenic