オブジェクトからその親のプロパティにアクセスできるかどうかを知りたいのですが、これは明示的な例です。たとえば、People
オブジェクトにオブジェクトの配列があるとしGroup
ます。
このGroup
オブジェクトでは、すべてPeople
が同じアドレスを持っているのでGroup
、各People
オブジェクトではなく、で宣言すると便利ですが、コレクションを解析せずにアクセスするにはどうすればよいですか?
function Group() {
this.address = 'EveryWhere';
this.Collection = [];
}
function People(data) {
this.name = data.name;
this.getAddress = function() {
return this.address; //how to access it (declared in the Group object)?
}
}
var Serie = new Group();
var John = new People();
Serie.Collection.push(John);
console.log(John.getAddress());
新しいGroup
オブジェクトをに割り当てることで、継承を模倣できます。People.prototype
function Group() {
this.address = 'EveryWhere';
this.Collection = [];
}
function People(data) {
this.name = data.name;
this.getAddress = function() {
return this.address; //how to access it (declared in the Group object)?
}
};
People.prototype = new Group();
var Serie = new Group();
var John = new People({name:"John"});
console.log(John.getAddress());
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加