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

私にとっては機能していますが、「システム」が言うように、プロトはどこにでもあるわけではありません。

4

2 に答える 2

3

いいえ、コンストラクターを配列にする必要はありませんCustomArray(配列メソッドを持ちます)。関連するのはこれです:

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

どのように ECMAScript 5 がまだ配列のサブクラス化を許可していないかを読んでください。上記の両方のプロパティは簡単に実装できますが、実際の問題はlengthプロパティにあります (これも機能しませんnewCustomArray)。

于 2013-02-20T01:49:01.277 に答える
0

サブクラス配列に似たものを取得する方法に従って、これを試してみましたが、私にとっては最高ではありませんが、使用可能です。

あなたが思うこと?

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 に答える