2013-06-24 39 views
0

一個新的對象考慮下面的JavaScript代碼:如何從現有對象的構造函數在javascript

if (oo instanceof MyType) { 
    var newObj = new oo.constructor; 

    // suppose the following check should be ok but it is not 
    // newObj should have same constructor as its original oo. 

    if (newObj instanceof MyType) { 
     // do something 
    } 
} 

我不能達到「做一些事情」。怎麼了?

+2

oo.constr ** A **構造函數()? – NicoSantangelo

+1

我將'constrActor'改爲'constrUctor',並且確定。 –

+1

我認爲他的意思是收縮... – elclanrs

回答

3

好吧,僅僅因爲你可能沒有閱讀評論,你在constructor(它說constractor)有一個錯字。

這工作:

function MyType() {} 
oo = new MyType(); 

if(oo instanceof MyType) 
    { 
     var newObj = new oo.constructor(); // The mistake was in this line 

     if(newObj instanceof MyType) 
     { 
      console.log("a contractor was killed by a constrictor while constructing"); 
     } 
    } 
+0

錯字不存在帖子問題編輯:S –

相關問題