JavaScript 클래스에서 부모 메서드를 호출 할 수 있지만 부모 클래스와 자식 클래스의 프로토 타입 메서드에 계속 액세스 할 수 있습니까?JavaScript 클래스에서 부모 메소드를 호출하지만 객체 인스턴스 내부의 프로토 타입 메소드에 액세스 할 수 있습니까?
var Base = function() {
this.baseMethod = function(){
return 'baseMethod';
};
this.baseInitMethod = function() {
return 'baseInitMethod';
}
}
Base.prototype.basePrototypeMethod = function() {
return "basePrototypeMethod";
};
var Specific = function() {
Base.call(this);
this.baseInitMethod = function() {
// call baseInitMethod from Base class
}
this.specificMethod = function(){
return 'specificMethod';
}
this.specificInitMethod = function() {
return this.basePrototypeMethod();
}
}
Specific.prototype.specificPrototypeMethod = function() {
return 'specificPrototypeMethod' + '-' + this.baseInitMethod();
}
for(var p in Base.prototype) {
Specific.prototype[p] = Base.prototype[p]
}
var s = new Specific();
console.log(s.baseMethod());
console.log(s.baseInitMethod());
console.log(s.basePrototypeMethod());
console.log(s.specificMethod());
console.log(s.specificInitMethod());
console.log(s.specificPrototypeMethod());
내가 특정 클래스 내부에 있지만, 그래서 모든 기능은 계속 작동 이상에서 호출하는 baseInitMethod 방법에서 기본 클래스에 baseInitMethod를 호출 할 : 다음 코드 예제입니다. 그게 가능하니? Specific
의 정의와,
var Base = function() {
};
Base.prototype.baseMethod = function() {
return 'baseMethod';
};
Base.prototype.baseInitMethod = function() {
return 'baseInitMethod';
};
Base.prototype.basePrototypeMethod = function() {
return "basePrototypeMethod";
};
var Specific = function() {
Base.apply(this, arguments);
};
Specific.prototype.baseInitMethod = function() {
Base.prototype.baseInitMethod.apply(this,arguments);
};
Specific.prototype.specificMethod = function() {
return 'specificMethod';
};
Specific.prototype.specificInitMethod = function() {
var basePrototypeMethodCallResult = Base.prototype.basePrototypeMethod.apply(this,arguments);
};
? 메소드를 공유하려면 두 메소드 모두에서 사용할 수있는 세 번째 클래스를 작성하십시오. – Alex
예를 들어 주시겠습니까? –