2014-10-03 1 views
0

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); 

}; 
+0

? 메소드를 공유하려면 두 메소드 모두에서 사용할 수있는 세 번째 클래스를 작성하십시오. – Alex

+0

예를 들어 주시겠습니까? –

답변

1

귀하의 Specific.prototype 객체가 Base.prototype 개체에서을 상속합니다.

for(var p in Base.prototype) { 
    Specific.prototype[p] = Base.prototype[p] 
} 

그러나 당신은 실제로 진짜 프로토 타입 체인 설정 Object.create를 사용해야합니다 : : 현재이 코드를 사용하여 객체의 모든 속성을 통해 복사하는

Specific.prototype = Object.create(Base.prototype); 

Specific.prototype.specificPrototypeMethod = function() { 
    return 'specificPrototypeMethod' + '-' + this.baseInitMethod(); 
} 

baseInitMethod에서 Base 클래스의 baseInitMethod를 호출하려고합니다. hod inside 특정 등급

예. 예. 모든 기능이 계속 작동 이상에서 호출

function Specific() { 
    Base.call(this); 

    var parentInitMethod = this.baseInitMethod; 
    this.baseInitMethod = function() { 
     // call baseInitMethod from Base class: 
     parentInitMethod.call(this /*, arguments…*/); 
    } 

    … 
} 

그래서 : 당신의 Specific 생성자에서 먼저 인스턴스의 속성을 덮어 쓰기 전에, BasebaseInitMethod 인스턴스 메소드를 얻을 필요가있다.

정확히 무슨 뜻인지 잘 모르겠습니다. specificPrototypeMethod은 항상 현재 인스턴스의 baseInitMethod을 호출합니다. SpecificBase에 정의 된 원본이 아닌 덮어 씁니다.

+1

Ohhhh man 나는 너무 어리 석다. :) 대답은 너무 간단하다. 특정 클래스에서 그 메소드를 오버라이드하기 전에 부모 메소드에 대한 레퍼런스를 생성해야했다. 대단히 감사합니다. 이제 나는 왜 당신이 106K 대표를 가지고 있는지 이해한다. 당신은 닌자입니다 :) 다시 한번 감사드립니다. –

1

은 당신이 할 필요가 무엇인가? 당신은 단순히 함수의 덮어 쓰기를 제거하면 당신은 기본 정의를 호출해야합니다 :

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(); // calls the Base definition only 

    this.specificMethod = function(){ 
    return 'specificMethod'; 
    } 

    this.specificInitMethod = function() { 

    return this.basePrototypeMethod(); 
    } 
} 
+0

나는 이것을 알고있다.하지만 당신이 new Specific()을 말할 때 생성자 내부에있는 모든 메소드가 다시 만들어지기 때문에 프로토 타입에있는 모든 메소드를 다시는 만들지 않기 때문에 프로토 타입에있는 메소드는 새로운 Specific()을 말할 때 다시 생성되지 않기 때문에 저는 프로토 타입에 있기를 원하지 않습니다. –

+0

정확합니다. 그리고베이스에서 사용 된 메소드를 원한다면 프로토 타입 안에 정의해야합니다. 그렇지 않으면, 그것은 단지 당신의 Base 클래스의 인스턴스 범위에서 정의됩니다. – jevgenig

+0

btw : 메서드가 다시 생성되지 않는 문제는 없습니다. "this"인스턴스를 통해 여전히 액세스 할 수 있기 때문입니다. 또한 레크리에이션에 필요하지 않은 것들이 JS 런타임 중에 재생성되지 않는 것이 좋지 않습니까? ;) – jevgenig

0

을 당신은 Specific 내부 BasebaseInitMethod 덮어 쓰기를하고, 왜 당신은 이제까지 기본 버전을 호출 할 것이다 : 여기

+0

baseInitMethod가 공용 메서드 외부에서 호출되어 정의와 같을 필요가 있기 때문에이 작업을 수행 할 수 없습니다. –

+0

괜찮 았어.하지만 너무 복잡하기 때문에이 over-engineered 접근법을 피하는 것을 고려해야한다. 당신이 옳은 것으로 표시 한 대답은 제가 말한 것을 정확히 나타냅니다. – Alex

0

"왜 항상 고전적인 행동을 모방하려고 노력하고 대신 프로토 타입 위임 패턴을 수용하는 대신 callapply을 사용하여 혼란에 빠지겠습니까?" 여기

내가 코드를 할 것입니다 : 확실히 당신이 휴식 상속의 규칙을 원하는

var Base = { 

    baseVariable1: "baseValue1", 

    baseVariable2: "baseValue2", 

    baseMethod: function() { 
     return 'baseMethod'; 
    }, 

    baseInitMethod: function() { 
     return 'baseInitMethod'; 
    } 
} 

var Specific = Object.create(Base); 

Specific.variable1 = "value1"; 

Specific.variable2 = "value2"; 

Specific.specificInitMethod = function() { 
    return 'specificInitMethod' + '-' + this.baseInitMethod(); 
} 

Specific.specificMethod = function() { 
    return 'specificMethod' + '-' + this.baseInitMethod(); 
} 

var s = Object.create(Specific); 

console.log(s.baseInitMethod()); 

console.log(s.baseVariable1); 

console.log(s.baseVariable2); 

console.log(s.variable1); 

console.log(s.variable2); 

console.log(s.baseMethod()); 

console.log(s.specificInitMethod()); 

console.log(s.specificMethod()); 
+0

이 접근 방식의 문제점은 기본 클래스 내에 변수를 가질 수없고 변수가 필요하다는 것입니다. –

+1

왜 안되니? – laruiss

+0

물론 편집 할 수있는 답변을 볼 수 있습니다. JS에는 클래스가 없습니다. (나는 ES6과'class'와'super' 키워드로도 JS의 실제 프로토 타입 특성을 감추는 키워드 일 뿐이라고 생각합니다.) 'Base'는 객체입니다. 필자의 경우에는 일반 객체이므로 함수, 프리미티브 또는 일반 객체를 가리킬 수있는 속성을 추가 할 수 있습니다. 함수는 JS이지만, 클래스가 아니라 객체이기 때문에 여전히 객체입니다. – laruiss

관련 문제