2013-02-20 81 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); 
相关问题