Javascript 对于子类数组,这是一个棘手的解决方案吗?

Javascript 对于子类数组,这是一个棘手的解决方案吗?,javascript,Javascript,今天,我在做一个新的SVG框架,我尝试对数组进行子类化以处理节点。。。几个小时后,我完成了这段代码(我只在Safari上进行了测试): 对我来说,这是可行的,但正如“系统”所说,proto并非无处不在。不,您不希望CustomArray构造函数成为数组(具有数组方法)。与此相关的是: newCustomArray instanceof Array; // false, should be true newCustomArray.forEach; // undefined, should be [

今天,我在做一个新的SVG框架,我尝试对数组进行子类化以处理节点。。。几个小时后,我完成了这段代码(我只在Safari上进行了测试):


对我来说,这是可行的,但正如“系统”所说,proto并非无处不在。

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

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

继续读下去。上面的两个属性都很容易实现,但实际问题是
length
属性(它对您的
newCustomArray
也不起作用)。

下面是一种获得类似于子类数组的方法,我尝试了这一方法,对我来说这不是最好的,但可用

你怎么想

listofcustomarray=[];
ListofCustomArray.fn={};
Array_prototype=Array.prototype;
数组=函数(){
//向列表中添加新的自定义数组
listofCustomArray.push([]);//新数组
ListofCustomArray[ListofCustomArray.length-1]。索引=ListofCustomArray.length-1;
//当前的最后一个
var arr=listofcustomarray[listofcustomarray.length-1];
对于(客户阵列列表中的j.fn){
对象定义属性(arr,j{
值:ListofCustomArray.fn[j]
});
}
返回arr
};
Array.extend=函数(名称,fnc){
ListofCustomArray.fn[名称]=fnc;

因为(i=0;i
)\uuuu proto\uuuu
是非标准的,所以它取决于您支持的环境。您不能“子类化”数组,因为通过
new
创建的对象只是一个普通对象(即对象的实例),只有真实数组具有特殊的自调整长度属性。只需设置对象的
[[Prototype]]
数组。prototype
只会为您提供方法。哦,您还发现
instanceOf
的无用性。它不会告诉您对象实际上是什么实例,只会告诉您在继承链的某个地方是否有引用所提供对象的构造函数属性。@RobG:不,是“构造函数”属性仍然与此无关。感谢您的评论@RobGYeha you's true,我错取了我的变量名hahaha(sh*t)
newCustomArray instanceof Array; // false, should be true
newCustomArray.forEach; // undefined, should be [].forEach
    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);