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

对我来说正在工作,但正如“系统”所说,proto并不无处不在。

4

2 回答 2

3

不,您不希望CustomArray构造函数成为 Array(具有数组方法)。相关的是:

newCustomArray instanceof Array; // false, should be true
newCustomArray.forEach; // undefined, should be [].forEach

阅读ECMAScript 5 如何仍然不允许对数组进行子类化。上面的两个属性都很容易实现,但实际的问题是length属性(这对你也不起作用newCustomArray)。

于 2013-02-20T01:49:01.277 回答
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);
于 2013-02-20T17:52:06.067 回答