저는 현재 JavaScript에 대한 이해를 깊게하고 있습니다. 특히 객체 상속과 관련하여 더욱 그렇습니다. 따라서 연습을 위해 API 클라이언트를 구현합니다. API는 상속이있는 JavaScript 객체 목록으로 변환하려는 객체 목록으로 구성된 JSON 객체를 반환합니다 (나중에 공통 객체를 Parent 객체에 추가 함).JSON에서 상속 된 JavaScript 객체를 얻는 방법은 무엇입니까?
저는 여기에 현재
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);
자바 스크립트 JSON에서 상속 개체를 만들 수있는 더 나은 방법이 있나요을 수행하는 작업에 최소한의 예입니다? 당신이 C++/C#/자바 세계 :)에서 오는처럼
덕분 배열 , 나는 그것이해야한다고 생각했던 것처럼 많이 본다. –