简体   繁体   中英

Equivalent of object using literal notation

See following class:

function availItem(xs, s, m, l, xl) {
            this.xs = xs;
            this.s = s;
            this.m = m;
            this.l = l;
            this.xl = xl;
        }

How can I declare the above class using JSON? I think It should be in following manner but problem is to pass argument.

var availItem = { 
               xs : xs,
                s : s,
                m : m,
                l : l,
                xl : xl


}

I want to use both in same manner like

var obj =new availItem(xs,s,b,l,xl);

There are no classes in JavaScript, only objects. The first method that you have for creating an object is often called an instantiated or constructed object.

function availItem(xs, s, m, l, xl) {
    this.xs = xs;
    this.s = s;
    this.m = m;
    this.l = l;
    this.xl = xl;
}

Objects defined in this manner can be instantiated with the new operator

var item = new availItem(...);

The second method creates an object using object literal notation which is almost, but not quite JSON. Most notably, the new operator does not work with object literals since they have no constructor (function).

If you want to use object literal notation, I suggest you follow the module pattern ( criticism for balance)

var availItem = availItem(xs, s, m, l, xl) {
    var my = {
        xs: xs,
        s:  s,
        m:  m,
        l:  l,
        xl: xl
    };

    // Add any methods that may be necessary
    my.method1 = function() { ... };

    // etc

    return my;
};

...

var item = availItem(...);

It's unclear why you want to use both methods for the same thing.

Try setting the properties in quotes. For instance:

{
    "xs" : xs,
    "s" : s,
    "m" : m,
    "l" : l,
    "xl" : xl
}

Those 2 pieces of code aren't the same.

The first one is a constructor that creates an object. You can use it such as:

var obj = new availItem(xs, s, m, l, xl); .

At this point obj is a JSON object.

The second one is a JSON object (what you would get from calling the former constructor) which is just data (it doesn't provide any particular functionality other than a reference to some data).

You didn't specify the reason why you need to pass parameters to the availItem object. The parameters can just be the values you assign to the object attribues:

var availItem = { 
               xs : param1,
                s : param2,
                m : param3,
                l : param4,
                xl : param5
}

Unfortunately, you can't. That's not really a class (there's no such thing in Javascript), it's a function, and JSON (which is just a data format) can't represent functions or function calls.

The closest you can get is what softcr suggests - an object literal with the correct properties. The quotes also matter - some JSON parsers will reject JSON if property names aren't quoted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM