简体   繁体   English

使用文字符号等效的对象

[英]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? 如何使用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. JavaScript中没有类,只有对象。 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 可以使用new运算符实例化以这种方式定义的对象

var item = new availItem(...);

The second method creates an object using object literal notation which is almost, but not quite JSON. 第二种方法使用对象文字表示法创建一个对象,该表示法几乎是JSON,但不是JSON。 Most notably, the new operator does not work with object literals since they have no constructor (function). 最值得注意的是, new运算符不使用对象文字,因为它们没有构造函数(函数)。

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. 此时, obj是一个JSON对象。

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). 第二个对象是一个JSON对象(从调用前一个构造函数得到的内容),它只是数据 (它不提供对某些数据的引用以外的任何特定功能)。

You didn't specify the reason why you need to pass parameters to the availItem object. 您没有指定需要将参数传递给availItem对象的原因。 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. 那实际上不是一个类(Javascript中没有这样的东西),它是一个函数,而JSON(只是一种数据格式)不能表示函数或函数调用。

The closest you can get is what softcr suggests - an object literal with the correct properties. 您可以得到的最接近的是softcr的建议 -具有正确属性的对象文字。 The quotes also matter - some JSON parsers will reject JSON if property names aren't quoted. 引号也很重要-如果未引用属性名称,则某些JSON解析器将拒绝JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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