2017-02-28 62 views
1

我正在寫一些擴展父類的JS,我想知道是否有方法可以告訴子類是否使用父方法而未調用它。理想情況下,我希望在父項的構造函數中運行檢查,以查看是否有任何子方法正在方法定義中使用父項的方法。javascript - 檢查子方法中是否使用父方法

我已經完成了一些研究,並且遇到了類似Object.getOwnPropertyNames()的東西,但我不確定我是否朝着正確的方向前進。

例如:

class Path { 

    constructor (name) { 
     // how can I check if addRelationship have been used? If possible. 
     this.relationships   = {}; 
     this.currentRelationship = ''; 
     this.path     = path; 
    } 

    addRelationship (relationship) { 
     // do something 
     this.currentRelationship = relationship.path; 

     return this; 
    } 

    makePath() { 
     let path = [this.path]; 

     if(this.currentRelationship) { 
      path.push(this.currentRelationship) 
     } 

     return path.join("/"); 
    } 

} 

class OnePath extends Path { 
    // ... 
    someMethodFromThatRelationship() { } 
} 

class TwoPath extends Path { 
    // ... 
} 

var onePath = new OnePath('one'); 
var twoPath = new TwoPath('two-path'); 

class SomeOtherPath extends Path { 

    one() { 
     return this.addRelationship(onePath); 
    } 

    two() { 
     return this.addRelationship(twoPath); 
    } 

} 

上面的例子中的想法是,如果addRelationship在任何引用的方法我可以檢查,如果是這樣,one()two()實際上是在調用之前註冊this.relationships.onethis.relationships.two。我希望我有道理。我很想知道這是否可能。

更新

上面的代碼的最終結果將是做到以下幾點能力:

let someOtherPath = new SomeOtherPath('some-other-path'); 

// now I can call 
someOtherPath.relationships.one.someMethodFromThatRelationship(); 

// and can also call the save method from the extended class 
someOtherPath.one().makePath(); 
// some-other-path/one 

// I can also just call 
someOtherPath.makePath(); 
// some-other-path 
+1

你想創建一個接口嗎? –

+0

@凱斯霍利迪最高興的是,是的。或者至少是某種形式的界面。 –

+1

你的[實際問題](http://meta.stackexchange.com/q/66377)是什麼?你只需要獲取子類的方法名稱,或者實際上只需要那些調用'addRelationship'的方法? – Bergi

回答

2

有沒有辦法判斷一個子類是使用父方法沒有調用它呢?

不知道什麼程序不調用它們就等於無法解析halting problem

1

我認爲你實際上在尋找的是一種更具說明性的方法來一次性創建關係及其伴隨的方法。不要使用太多的魔法(父級構造函數檢查它的子類代碼肯定會),但要明確。

class Path { 
    constructor (path) { 
     this.relationships   = {}; 
     this.currentRelationship = ''; 
     this.path     = path; 
    } 
    addRelationship (name, relationship) { 
     this.relationships[name] = relationship; 
     this[name] = function() { 
      // do something 
      this.currentRelationship = name; 
      return this.relationships[name]; 
     } 
     return this; 
    } 

    makePath() { 
     let path = this.path; 
     if (this.currentRelationship) { 
      path += "/" + this.relationships[this.currentRelationship].makePath(); 
     } 
     return path; 
    } 
} 

class SomeOtherPath extends Path { 
    constructor(name) { 
     super(name); 
     this.addRelationship("one", new OnePath('one')); 
     this.addRelationship("two", new TwoPath('two-path')); 
    } 
} 

甚至

class Path { 
    constructor (path, relationships = {}) { 
     this.relationships   = relationships; 
     this.currentRelationship = ''; 
     this.path     = path; 

     for (let const r in relationships) 
      this.addRelationship(r, relationships[r]); 
    } 
    … 
} 

class SomeOtherPath extends Path { 
    constructor(name) { 
     super(name, { 
      one: new OnePath('one'), 
      two: new TwoPath('two-path') 
     }); 
    } 
} 

也許你甚至不需要這些子類的任何更多,如果他們沒有其他方法或只實例化一次(如單身)。

請注意,上述方法將在構造函數的每個實例上創建新的方法和新的子路徑,如果您不希望自己也可以靜態地將聲明放在類上。只需使addRelationShip成爲靜態方法,即初始化默認的relationships對象並將該方法放在類「.prototype」上。模式的變化是無止境的。

你甚至可能想要嘗試使用建議的decorators feature的類。

相關問題