2016-12-10 4 views
0
class PathController { 
    constructor(){ 

    } 

    getMainPage(){ 
    alert("getMainPage"); 
    } 

    setPushState(){ 
    alert("setPushState"); 
    } 
} 

class MainMenu extends PathController { 
    constructor(){ 
    // call my PathController here 
    super(); 
    getMainPage(); 
    setPushState(); 
    } 
} 

let aMainMenu = new MainMenu(); 

내 의도는, 내 MAINMENU 생성자에서 내 getMainPage 및 setPushState를 호출하는 것입니다 내가 피곤 this.getMainPage 및 this.setPushState와 그것뿐만 아니라 작동하지 않는 생성자를 확장합니다. 누구든지 내게 전화하는 법을 말해 줄 수 있니?통화 기능은

답변

0

한 가지 방법은 PathController 부모 생성자에서 함수를 호출 super()에 속성 이름을 통과하는 것입니다

class PathController { 
 
    constructor(fromMainMenu, ...props) { 
 
    if (fromMainMenu) { 
 
     for (let fn of props) { 
 
     this[fn]() 
 
     } 
 
    } 
 
    } 
 

 
    getMainPage(){ 
 
    alert("getMainPage"); 
 
    } 
 

 
    setPushState(){ 
 
    alert("setPushState"); 
 
    } 
 
} 
 

 
class MainMenu extends PathController { 
 
    constructor() { 
 
    // call my PathController here 
 
    super(true, "getMainPage", "setPushState"); 
 
    } 
 
} 
 

 
let aMainMenu = new MainMenu();

1

우리가 현재 생성자에 있기 때문에 슈퍼가 "this"입니다. 방법은 다음과 같습니다.

class PathController { 
    constructor(){ 

    } 

    getMainPage(){ 
    alert("getMainPage"); 
    } 

    setPushState(){ 
    alert("setPushState"); 
    } 
} 

class MainMenu extends PathController { 
    constructor(){ 
    // call my PathController here 
    super(); 
    super.getMainPage(); 
    super.setPushState(); 
    } 
} 

let aMainMenu = new MainMenu(); 

일단 생성자 외부에 있으면 "this.getMainPage();"를 사용합니다.