Currently, I am looking to enhance my knowledge of JavaScript, particularly in the area of object inheritance. To practice this concept, I have decided to create a client for an API. This API provides a JSON object containing lists of objects that I aim to convert into lists of JavaScript objects with inheritance capabilities (to later add common functions to the Parent object).
Below is a simplified example of my current approach:
json=JSON.parse('{"lista":[{"one":"","two":""},{"one":"","two":""}],"listb":[{"one":"","three":""},{"one":"","three":""}]}')
function Parent(parent) {
this.one = parent.one;
}
function A(a) {
Parent.call(this,a);
this.two = a.two;
}
A.prototype = Object.create(Parent.prototype);
A.prototype.constructor = A;
function B(b) {
Parent.call(this,b);
this.three = b.three;
}
B.prototype = Object.create(Parent.prototype);
B.prototype.constructor = B;
function Collection(collection) {
this.lista = (typeof collection.lista !== "undefined" ? collection.lista.map(function (a) {
return new A(a)
}) : undefined);
this.listb = (typeof collection.listb !== "undefined" ? collection.listb.map(function (b) {
return new B(b)
}) : undefined);
}
collection=new Collection(json);
Is there a more efficient method to generate JavaScript objects with inheritance from JSON?