简体   繁体   English

没有instanciation的JavaScript类定义

[英]JavaScript class definition without instanciation

I'd like to define an empty data class in JS to fill it with JSON afterwards. 我想在JS中定义一个空数据类,然后用JSON填充它。 In c# I'd do something like that : 在c#中,我会做类似的事情:

class Person {
    string name;
    int age;
    Person[] children;
    var whatever;
}

This doesn't work, the IDE doesn't like it : "Unexpected token. A constructor, method, accessor, or property was expected". 这不起作用,IDE不喜欢它:“意外的令牌。期望构造函数,方法,访问器或属性”。

I've tried 我试过了

class Person {
    var name;
    var age;
    var children;
    var whatever;
}

but I have no experience with JS, and everywhere I see that declared variables are defined right away. 但是我没有使用JS的经验,而且我看到所有声明的变量都是立即定义的。

I don't understand the error since I believe those are properties, and I don't know the behaviour of the JSON deserializer regarding arrays of objects, knowing said objects might be missing properties in the JSON but I'd still want them to be declared (but not set) in the JS object created from the string. 我不明白错误,因为我认为这些是属性,我不知道JSON反序列化器关于对象数组的行为,知道所述对象可能缺少JSON中的属性但我仍然希望它们是在从字符串创建的JS对象中声明(但未设置)。

How can I make things work ? 我怎样才能让事情奏效?

You would need to make it an object rather than a class, and then fill the properties in when you receive your JSON. 您需要将其设为object而不是类,然后在收到JSON时填充属性。

You should use something like: 你应该使用类似的东西:

var Person = {
    name: '',
    age: '',
    children: '',
    whatever: ''
}

You can then access the properties of the Person object with Person.name etc. 然后,您可以使用Person.name等访问Person对象的属性。

Deserializing will be as simple as 反序列化将非常简单

Person = FunctionThatGivesYouAPersonObjectInJSON();

JSON is the notation used for Java Script objects, so you don't need to store these variables in a class. JSON是用于Java Script对象的表示法,因此您不需要将这些变量存储在类中。 Just store it in a normal object. 只需将其存储在普通对象中即可。 If you are reciving a JSON object from ajax request for example you can store it as a normal variable. 例如,如果要从ajax请求中恢复JSON对象,则可以将其存储为普通变量。

So just use that object. 所以只需使用该对象。

You define a class of this way. 你定义了这种方式的类。

class Person{
 constructor(name, age, children, whatever) {
  this._name = name;
  this._age = age;
  this._children = children;
  this._whatever = whatever;
  }
}

To instantiate, you do this. 要实例化,请执行此操作。

var chris = new Person('Chris', 25, 2, 1)

In ECMAScript6 you can implement Classes with JavaScript: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes 在ECMAScript6中,您可以使用JavaScript实现类: https//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes

So you will have to change your code to: 因此,您必须将代码更改为:

class Person{
  constructor(age, name) {
    this.age = age;
    this.name = name;
  }
}

Also, problem might be in your IDE settings. 此外,问题可能出在IDE设置中。 Usually in IDE you can select which language version you would like to use. 通常在IDE中,您可以选择要使用的语言版本。 For Example in WebStorm you can select in Settings: 例如,在WebStorm中,您可以在“设置”中进行选择:

在此输入图像描述

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

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