내 프로젝트를위한 매우 간단한 상속 패턴을 얻으려는 중이며 기본 클래스에서 특수 클래스로 확장하려고합니다. 그러나 내 특수 클래스의 특성은 부모 특성에 의해 덮어 쓰여지고 있습니다.속성이있는 JS 객체 상속
왜 그런가요? 어떻게 고칠 수 있습니까?
덕분에,
function Ship(className, x, y){
this.className = className;
this.x = x;
this.y = y;
this.speed = 0;
}
function Corvette(className, x, y){
this.className = className;
this.x = x;
this.y = y;
this.speed = 100;
Ship.call(this, className, x, y)
}
Corvette.prototype = Object.create(Ship.prototype);
var ship = new Ship("Biggie", 50, 50);
var corvette = new Corvette("Smallish", 50, 50);
console.log(Corvette.className) // "Smallish" - correct via parameter.
console.log(Corvette.speed) // should be 100, is 0 - not correct, "static" from parent attribute
console.log(Corvette.constructor.name) // Ship
저장 너 자신은 오래된 "클래스"시스템의 골칫거리이며 [ES6 클래스] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)를 사용하십시오. –
속성 조회는 일반적으로 첫 번째 레벨에서 발생합니다 prot에 반대하다 Ship.call (this, className, x, y)'이 호출은 속도 값을'0'으로 바꿀 것입니다. –