3
내가 http://ejohn.org/blog/simple-javascript-inheritance/에서 간단한 상속을 시도하고 나는 다음과 같은 코드를 가지고 : 나는 위를 실행하면간단한 상속
var resources = [];
var Entity = Class.extend({
pos : {
x: 0,
y: 0
},
init : function(x, y) {
this.pos.x = x;
this.pos.y = y;
},
toString : function() {
return this.pos.x + ' | ' + this.pos.y;
}
});
var bFunc = Entity.extend({
init : function(x, y) {
this._super(x, y)
}
});
var cFunc = Entity.extend({
init : function(x, y) {
this._super(x, y)
}
});
var Func = Class.extend({
init : function() {
this.b = new bFunc(1, 10);
resources.push(this.b);
this.c = new cFunc(5, 10);
resources.push(this.c);
},
print : function() {
for(var i in resources) {
console.log(resources[i].toString());
}
}
});
var func = new Func();
func.print();
, 나는 콘솔에서 볼 :
5 | 10 5 | 10
을하지만 설정하고 :
this.b = new bFunc(1, 10); // 1, 10
resources.push(this.b);
this.c = new cFunc(5, 10); // 5, 10
resources.push(this.c);
왜 다음과 같은 메시지가 표시되지 않습니까?
1 | 10 5 | 10
감사합니다,하지만 난 당신의 예제의 시도입니다. 첫 번째 :
두 번째 : – vuohu어떤 종류의 개체가 "리소스"입니까? 개체의 속성을 열거하면 모든 속성을 가져옵니다. 그 안에 저장된 항목 만이 아닙니다. 그래서 나는 두 번째 형식을 추천한다. – codenheim
리소스는 모두 내 클래스 (b, c)이고 루프를 사용하면 모든 클래스를 리소스에서 인쇄 할 수 있지만 하나의 클래스 집합 인 경우 this.b = new bFunc (1, 10)) - 더 이상 모든 Class get의 마지막 클래스 x, y (Class가 x, y가 5 인 클래스가 2 인 경우 1 - 10, 5 | 10, 5 | 3 종류의 지문 - 15 | 15, 15 | 15, 15 | 15. – vuohu