2014-01-14 42 views
0
/** 
* adds an entry to the list 
* 
* @param data {Object} 
* @return this 
*/ 
ElementList.prototype.addEntry = function(data){ 
    if (!data) return this; 

    data['type'] = this.children_type; 

    // add the entry to the current elements 
    this.element_list.push(new FavoriteEntry(data)); 

    this.refresh(); 

    return this; 
}; 

/** 
* favorites extend ElementList 
* 
* @param setting_list 
* @constructor 
*/ 
function FavoriteList(setting_list){ 
    ElementList.call(this, setting_list); 
} 

FavoriteList.prototype = new ElementList(); 
FavoriteList.constructor = FavoriteList; 

所以這是一個我的教育項目的短代碼snipplet。 我想要做的是減少重複的代碼,所以我創建了一個通用的元素列表對象 所以Javascript對象繼承如何獲取子實例名稱

  • 爲例FavoriteList inherties父對象原型
  • 施工人員正指向Childobject
  • 的父類的構造是在孩子內部呼叫。

這工作得完全沒有我的問題是

// add the entry to the current elements 
this.element_list.push(new FavoriteEntry(data)); 

這應該創建一個對象的新實例根據孩子所以因此我需要的是調用了父子實例的名稱方法

我試圖 - this.constructor(指向父) - this.constructor.name - 這個的instanceof FavoriteList(作品)

因爲我不想傳遞一個名字,我認爲通過instanceof「options」迭代並不是很聰明。

我想問一些見解,我可以如何訪問父元素方法主體中的childs實例名稱。

請只有明確的答案!我已經閱讀了解決方法!如果這是不可能只是這麼說:)

THX提前:)

+1

這是不可能的。 –

+0

認爲如此:/ thx,所以一個醜陋的解決方案,它是 – shadowdroid

+0

我很困惑:所以你做這樣的事情:'var favList = new FavoriteList(「xyz」); favList.addEntry(「some data」);'你想訪問'addEntry'內的'favList'? – basilikum

回答

1
this.element_list.push(new FavoriteEntry(data)); 

這應該創建一個對象的新實例根據孩子這麼 因此我需要的名字子方法調用 父方法

不,您似乎不需要知道名稱。您只需要一個輔助函數來生成新的Entry實例,該實例可以被覆蓋以生成更多特定的條目。也許你已經這樣做,通過傳遞children_typedata ...

我嘗試 - this.constructor(指向父)

,如果你正確地設置constructor它應該工作。更改您的代碼

FavoriteList.prototype.constructor = FavoriteList; 
//   ^^^^^^^^^^ 

此外,你可能想use Object.createinstead of new建立原型鏈。

+0

啊,好吧,有道理:)現在就像一個魅力。 我已經改變它爲ecma標準Object.create我只是不想重新發布代碼:) thx爲輸入:) – shadowdroid

1

我不確定我是否完全理解,但代碼new FaforiteEntry應根據當前對象類型創建FororiteEntry或其他類型。

也許下面的例子可以幫助你:

var ElementList = function(args) { 
    this.element_list = []; 
} 
ElementList.prototype.addEntry = function(args) { 
    this.element_list.push(new this.entryType(args.val)); 
}; 
//will create element_list items of type String 
ElementList.prototype.entryType = String; 

function FavoriteList(args) { 
    ElementList.call(this, args); 
} 
FavoriteList.prototype = Object.create(ElementList.prototype); 
FavoriteList.constructor = FavoriteList; 
//will create element_list items of type Array 
FavoriteList.prototype.entryType = Array; 

//adding entries to f would create items of type Array 
var f = new FavoriteList(); 
f.addEntry({val: 2}); 
console.log(f.element_list);//[[undefined, undefined]] 
//adding entries to e would create items of type String 
var e = new ElementList(); 
e.addEntry({val: 2}); 
console.log(e.element_list);//[ String { 0="2"... 
+0

thx但Bergi給了我我需要的答案。我必須分配obj.prototype.constructor,以便可以訪問調用通用對象的子對象的名稱。 :) 'var call = this.constructor.name.replace('List','')+「Entry」; //項添加到當前元素 this.element_list [data.id] =新窗口[電話](數據);' 這就是我想做的事:) – shadowdroid

+1

@shadowdroid我明白了,請注意,變量FavoriteList被分配給我的代碼中的無名函數,並且this.name不會返回您所期望的。 – HMR

1

簡單的代碼示例:

function Parent(){ 
     // custom properties 
    } 

    Parent.prototype.getInstanceName = function(){ 
     for (var instance in window){ 
      if (window[instance] === this){ 
       return instance; 
      } 
     } 
    }; 

    var child = new Parent(); 

    console.log(child.getInstanceName()); // outputs: "child" 
+1

@shadowdroid注意:如果實例是在根中創建的,則此實現有效。 – shmuli