2017-01-28 4 views
0

나는이 질문을 여러 번 물어 본다.하지만 여전히 내 머리를 잡지 못했다. 자바 스크립트 oop를 사용하고 있는데 부모 클래스 함수 this.returnResult을 자식 클래스 함수 this.fullArr에서 호출해야합니다.자식 클래스의 oop 호출 부모 함수

function parantCls(){ 
    this.sCus = []; 
    this.aCus = []; 
    this.response; 


    this.returnResult = function(msg){ 
     this.response = { 
      result : msg 
     }; 

     return this; 
    } 
} 

function resonse(){ 
    parantCls.apply(this, arguments); 

    this.fullArr = function(){ 
     // call parent function 
     parantCls.prototype.returnResult(this,'setting customField should be array not ' + typeof this.sCus); 
     return this.response; 
    } 
} 

resonse.prototype = new parantCls(); 

parantCls.prototype.returnResult(this,'setting customField should be array not ' + typeof this.sCus);이 작동하지 않습니다. 메신저이

parantCls.prototype.returnResult.call(this,'setting customField should be array not ' + typeof this.sCus);

같은 call and apply을 사용하지만 여전히 작동하지 않습니다. 문제가 무엇입니까

+0

는 ('parantCls.prototype.returnResult.call (이는 ...') (근무해야 –

+0

결과 : 스택 스 니펫을 사용하여 ** runnable ** [mcve]로 질문을 업데이트하십시오. ([[>] 도구 모음 버튼) –

답변

2

프로토 타입 체인을 사용하여 올바르게 상속하는 경우.

이 그런데

this.returnResult('setting customField should be array not ' + typeof this.sCus) 

작동합니다, 당신의 상속은 잘못된 보인다. 이 형식을 사용하십시오. 당신이 할 수 불필요하게 복잡하지만 당신이 마지막에, '코드를 fullArr`를 호출하는 방법에 따라

var Subclass = function() { 
    Superclass.call(this); 
}; 

Subclass.prototype = Object.create(Superclass.prototype); 
Subclass.prototype.constructor = Subclass; 

Subclass.prototype.someMethod = function (value) { 
    this.x = value; 
}; 
+0

* "By this is the return to return 당신의 상속은 틀린 것처럼 보입니다. "* 프로토 타입 함수를 사용하지 않기 때문에 * 틀린 것은 아닙니다. 그러나 그렇습니다. 완전히 설정하는 것이 좋습니다.주의 : 위의 중요한 'var' ES2015 구문에 대해 언급하고 싶을 수도 있습니다. (마지막에'; '가 없습니다.) –

+1

@TJCrowder 매우 조심합니다. :-) – gzc

관련 문제