2013-02-20 23 views
1

今天上了一個新的SVG框架我做我試圖子陣列的工作與節點工作......並在幾個小時後,我用這個代碼(我只在Safari瀏覽器進行測試)完成:這是一個棘手的解決方案子類數組?

customArray=function(){ 
    // Do the thing 
    this.__proto__= Array.prototype; 

    // Add some prototypes to the main customArray 
    this.f1=function(){console.log(1)}; // f1 custom function 
    this.f2=function(){console.log(2)}; // f2 custom function 
}; 

newCustomArray=new customArray(); 
newCustomArray.f3=function(){console.log(3)} // f3 function only for newCustomArray 

console.log(newCustomArray instanceof Array); // true 
console.log([] instanceof Array);    // true 
console.log("---------------------"); 
console.log(newCustomArray.f1);    // function(){console.log(1)}; 
console.log(newCustomArray.f2);    // function(){console.log(2)}; 
console.log(newCustomArray.f3);    // function(){console.log(3)}; 
console.log([].f1);       // undefined 
console.log([].f2);       // undefined 
console.log([].f3);       // undefined 
console.log("---------------------"); 
console.log(newCustomArray.forEach);   // Native function 
console.log([].forEach);      // Native function 

對我來說是工作,但作爲「系統」說原型並不是無處不在。

+0

'.__ proto__'是非標準的,所以它取決於其環境需要支持。 – 2013-02-20 01:49:07

+0

非常感謝很多 – microbians 2013-02-20 01:53:01

+0

由於通過'new'創建的對象只是一個普通對象(即Object的實例),因此不能「子類化」Array,只有真實數組才具有特殊的自我調整長度屬性。簡單地將一個對象的[[Prototype]]設置爲'Array.prototype'只會讓你獲得方法。 – RobG 2013-02-20 03:06:27

回答

3

不,你不希望CustomArray構造函數是一個數組(有數組方法)。什麼是相關的是這樣的:

閱讀上How ECMAScript 5 still does not allow to subclass an array。上述兩個屬性都很容易實現,但實際問題是length屬性(它也不適用於您的newCustomArray)。

+0

葉哈你是真的,接縫我misktake我的var名hahaha(sh * t) – microbians 2013-02-20 01:51:49

0

接下來有一種方法可以得到類似於子類Array的東西,我試試這個,對我來說不是最好的,但可用。

你覺得呢?

http://jsfiddle.net/microbians/837rv/

listOfCustomArrays =[]; 
    listOfCustomArrays.fn={}; 
    Array_prototype=Array.prototype; 

    Array=function(){ 

     // Add a new custom array to the list 
     listOfCustomArrays.push([]); // NEW ARRAY 
     listOfCustomArrays[listOfCustomArrays.length-1].index=listOfCustomArrays.length-1; 

     // The the current last 
     var arr=listOfCustomArrays[listOfCustomArrays.length-1]; 

     for (j in listOfCustomArrays.fn) { 
      Object.defineProperty(arr, j, { 
       value: listOfCustomArrays.fn[j] 
      }); 
     } 

     return arr 
    }; 

    Array.extend=function(name,fnc) { 
     listOfCustomArrays.fn[name]=fnc; 
     for (i=0; i<listOfCustomArrays.length; i++) { 
      Object.defineProperty(listOfCustomArrays[i], name, { 
       value: listOfCustomArrays.fn[name] 
      }); 
     } 
    } 

    Array.prototype=Array_prototype; 

    newCustomArray=new Array(); 

    //Array.extend('f1', function(){console.log(1)}); 
    Array.prototype.f1=function(){console.log('f1:prototyped')}; 

    Array.extend('f2', function(){console.log('f2:extended')}); 
    Array.extend('f3', function(){console.log('f3:extended')}); 

    newCustomArray2=new Array(); 
    Array.extend('f4', function(){console.log('f4:extended')}); 

    console.log(typeof Array); 
    console.log(typeof []); 
    console.log("---------------------"); 
    console.log(newCustomArray.f1); 
    console.log(newCustomArray.f2); 
    console.log(newCustomArray.f3); 
    console.log(newCustomArray.f4); 
    console.log("---------------------"); 
    console.log(newCustomArray2.f1); 
    console.log(newCustomArray2.f2); 
    console.log(newCustomArray2.f3); 
    console.log(newCustomArray2.f4); 
    console.log("---------------------"); 
    console.log([].f1); 
    console.log([].f2); 
    console.log([].f3); 
    console.log([].f4); 
    console.log("---------------------"); 
    console.log(newCustomArray.forEach); 
    console.log(newCustomArray2.forEach); 
    console.log([].forEach); 
    console.log(Array.prototype.forEach); 
相關問題