简体   繁体   English

Javascript-在同一JSON字符串中打包父级和子级的属性

[英]Javascript - Pack properties of parent and child in the same JSON string

I have a javascript scenario where I have created a base class and a derived class and wish to pack the total set of properties into a JSON-string with JSON.stringify(). 我有一个javascript场景,其中创建了一个基类和一个派生类,并希望使用JSON.stringify()将全部属性打包到一个JSON-string中。

When I use the equivalent to the code below I only get the child-object's properties when I run "toString()" on one of the DerivedClass instances: 当我使用与下面的代码等效的代码时,仅在其中一个DerivedClass实例上运行“ toString()”时,才获得子对象的属性:

function BaseClass() {
    this.version = "0.0.0";
    this.time = Date.now();
    this.type = this.constructor.name;
}

BaseClass.prototype.BaseClassException = function(message) {
    this.message = message;
}

BaseClass.prototype.toString = function() {
    return JSON.stringify(this);
}

BaseClass.parse = function(jsonString) {
    var json = JSON.parse(jsonString);

    switch(json.type) {
    case "DerivedClass1":
        return new DerivedClass1();
    case "DerivedClass2":
        return new DerivedClass2();
    default:
        throw new BaseClassException("No compatible type found when parsing: " + jsonString);
}

function DerivedClass1(prop1, prop2) {
    this.prop1 = prop1;
    this.prop2 = prop2;
    this.type = this.constructor.name;
}
DerivedClass1.prototype = new BaseClass();
DerivedClass1.prototype.constructor = DerivedClass1;

function DerivedClass2(prop3) {
    this.prop3 = prop3;
}
DerivedClass2.prototype = new BaseClass();
DerivedClass2.prototype.constructor = DerivedClass2;


// Test
var dc1 = new DerivedClass1("A", "B");
console.log(dc1.toString());  // Returns JSON-string with properties of DerivedClass1, but not BaseClass

There will be several different derived classes. 将有几种不同的派生类。 While I do know that js doesn't really support classes I would still like to pack all the properties from the base and child objects in the same JSON-string. 虽然我确实知道js并不真正支持类,但我仍然希望将基础对象和子对象的所有属性打包在同一JSON字符串中。 The structure is necessary to correlate to the other nodes of the total system, ie all properties need to be present. 该结构对于与整个系统的其他节点相关是必不可少的,即所有属性都必须存在。

If anyone at the same time has the knowledge of nudging me in the correct direction to understand the link between the child object and parent object in order for me to better understand the "inheritance" part of js I'd be really thankful as well. 如果同时有人知道将我引导到正确的方向以理解子对象和父对象之间的链接,以便让我更好地理解js的“继承”部分,我也将非常感谢。 I more used to strict oo-languages so I'd be happy to learn. 我更习惯于严格的oo语言,所以我很乐于学习。

There are two things which I can readily suggest. 我很容易建议两件事。

  1. To invoke the base class constructor, you have to invoke it manually like this 要调用基类构造函数,您必须像这样手动调用它

     function DerivedClass1(prop1, prop2) { BaseClass.call(this); this.prop1 = prop1; this.prop2 = prop2; this.type = this.constructor.name; } 

    We invoke the parent constructor function, with the current object. 我们使用当前对象调用父构造函数。 The important thing to note here is that, we are setting the current context to the object of type DerivedClass1 . 这里要注意的重要一点是,我们正在将当前上下文设置为DerivedClass1类型的对象。

  2. To actually do prototypal inheritance, you need to use the base class's prototype, not the object. 要真正进行原型继承,您需要使用基类的原型,而不是对象。

     DerivedClass1.prototype = Object.create(BaseClass.prototype); 

    In your case, BaseClass 's constructor doesn't depend on any arguments. 在您的情况下, BaseClass的构造函数不依赖任何参数。 So, doing DerivedClass1.prototype = new BaseClass(); 因此,执行DerivedClass1.prototype = new BaseClass(); will not make a big difference. 不会有太大的不同。 But it is always better to depend only on the Parent constructor's prototype. 但是总是最好仅依赖于Parent构造函数的原型。 Read more about using Object.create for inheritance, in this wonderful answer . 在这个精彩的答案中 ,了解有关使用Object.create进行继承的更多信息。

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

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