2016-12-10 12 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

私たちが現在コンストラクタにいるので、superはあなたの "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を();」

関連する問題