views:

82

answers:

4

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);
A: 

Try setting the properties in quotes. For instance:

{
    "xs" : xs,
    "s" : s,
    "m" : m,
    "l" : l,
    "xl" : xl
}
softcr
How to define xs,s,m... as argument?
Brij
+1  A: 

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
}
Luca Matteis
"At this point `obj` is a JSON object" - do you mean "is a Javascript object"?
Sam Stokes
yeah whatever... `JSON object` seems more of a hip term to use.
Luca Matteis
A hip term, maybe. An incorrect term, definitely.
Andy E
actually it's not an incorrect term. JSON object is JavaScript Object Notation Object... so it's an object that uses the JavaScript Object Notation... so I'm right and you're wrong! And you call yourself **a javascript coder**?
Luca Matteis
But the JSON as in "data transportation format" can't contain functions, a *Javascript object literal* can.
deceze
Luca, see my comment to the OP. JSON is not a hip term that you can apply to anything. JSON is JSON and Object Literal Notation is Object Literal Notation. And as I said, the premise of calling anything JSON is that it **is** a string. So I'm afraid **you are wrong** and Andy is right :)
Sean Kinsey
@Luca: funniest thing I've heard all day, but it is only 10:30am here :-) As deceze said, JSON is a "data transportation format". Your answer says `obj`, created from a constructor using the `new` statement, is a JSON object. That simply isn't true, it's not in JSON format -- it's a Javascript object. The second one isn't a JSON object either, it's just an object literal. I suggest you read more about JSON at http://www.json.org/.
Andy E
+1  A: 

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.

Sam Stokes
+3  A: 

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.

Justin Johnson