子类化Javascript数组。 TypeError:Array.prototype.toString不是通用的

时间:2010-07-16 02:46:41

标签: javascript inheritance

是否可以从javascript Arrays中继承和继承?

我想拥有自己的自定义Array对象,该对象具有Array的所有功能,但包含其他属性。如果实例是我的CustomArray,我会使用myobj instanceof CustomArray来执行特定的操作。

在尝试子类化并遇到一些问题之后,我发现这篇Dean Edwards文章指出使用Array对象执行此操作无法正常工作。事实证明,Internet Explorer无法正确处理它。但我也发现了其他问题(目前只在Chrome中测试过)。

以下是一些示例代码:

/** 
 *  Inherit the prototype methods from one constructor into another 
 *  Borrowed from Google Closure Library 
 */
function inherits(childCtor, parentCtor) {
    function tempCtor() {};
    tempCtor.prototype = parentCtor.prototype;
    childCtor.superClass_ = parentCtor.prototype;
    childCtor.prototype = new tempCtor();
    childCtor.prototype.constructor = childCtor;
},

// Custom class that extends Array class
function CustomArray() {
    Array.apply(this, arguments);
}
inherits(CustomArray,Array);

array = new Array(1,2,3);
custom = new CustomArray(1,2,3);

在Chrome控制台中输入以下内容即可显示此输出:

> custom
[]
> array
[1, 2, 3]
> custom.toString()
TypeError: Array.prototype.toString is not generic
> array.toString()
"1,2,3"
> custom.slice(1)
[]
> array.slice(1)
[2, 3]
> custom.push(1)
1
> custom.toString()
TypeError: Array.prototype.toString is not generic
> custom
[1]

显然,对象的行为不一样。我应该放弃这种方法,还是有办法实现myobj instanceof CustomArray的目标?

7 个答案:

答案 0 :(得分:33)

Juriy Zaytsev(@kangax)就在今天发表了一篇关于这个主题的非常好的文章。

他探讨了各种替代方案,如Dean Edwards iframe借用技术,直接对象扩展,原型扩展以及ECMAScript 5访问器属性的使用。

最后没有完美的实施,每个都有自己的优点和缺点。

绝对是一本非常好的读物:

答案 1 :(得分:23)

ES6

class SubArray extends Array {
    last() {
        return this[this.length - 1];
    }
}
var sub = new SubArray(1, 2, 3);
sub // [1, 2, 3]
sub instanceof SubArray; // true
sub instanceof Array; // true

原始答案:(不推荐,可能导致performance issues

从接受的答案中提到的article复制粘贴以获得更多可见性

使用__proto__

function SubArray() {
  var arr = [ ];
  arr.push.apply(arr, arguments);
  arr.__proto__ = SubArray.prototype;
  return arr;
}
SubArray.prototype = new Array;

现在,您可以将方法添加到SubArray

SubArray.prototype.last = function() {
  return this[this.length - 1];
};

像普通数组一样初始化

var sub = new SubArray(1, 2, 3);

表现得像普通阵列

sub instanceof SubArray; // true
sub instanceof Array; // true

答案 2 :(得分:1)

我之前尝试过这样的事情;一般来说,它不会发生。但是,您可以通过在内部应用Array.prototype方法来伪造它。此CustomArray课程虽然仅在Chrome中进行了测试,但却实现了标准push和自定义方法last。 (不知怎的,这种方法在xD时我从未实际发生过)

function CustomArray() {
    this.push = function () {
        Array.prototype.push.apply(this, arguments);
    }
    this.last = function () {
        return this[this.length - 1];
    }
    this.push.apply(this, arguments); // implement "new CustomArray(1,2,3)"
}
a = new CustomArray(1,2,3);
alert(a.last()); // 3
a.push(4);
alert(a.last()); // 4

您打算将自定义实现中的任何Array方法都必须手动实现,尽管您可能只是聪明并使用循环,因为我们的自定义push中发生的事情非常通用。

答案 3 :(得分:1)

这是一个应该适用于ie9和更高版本的完整示例。对于< = ie8,你必须实现Array.from,Array.isArray等的替代方法。这个例子:

  • 将Array子类放在自己的闭包(或命名空间)中,以避免冲突和名称空间污染。
  • 从本机Array类继承所有原型和属性。
  • 显示如何定义其他属性和原型方法。

如果您可以使用ES6,则应使用发布的class SubArray extends Array方法laggingreflex。

以下是从Arrays继承和继承的基本要素。下面的摘录是完整的例子。

///Collections functions as a namespace.     
///_NativeArray to prevent naming conflicts.  All references to Array in this closure are to the Array function declared inside.     
var Collections = (function (_NativeArray) {
    //__proto__ is deprecated but Object.xxxPrototypeOf isn't as widely supported. '
    var setProtoOf = (Object.setPrototypeOf || function (ob, proto) { ob.__proto__ = proto; return ob; });
    var getProtoOf = (Object.getPrototypeOf || function (ob) { return ob.__proto__; });        

    function Array() {          
        var arr = new (Function.prototype.bind.apply(_NativeArray, [null].concat([].slice.call(arguments))))();           
        setProtoOf(arr, getProtoOf(this));     
        return arr;
    }

    Array.prototype = Object.create(_NativeArray.prototype, { constructor: { value: Array } });
    Array.from = _NativeArray.from; 
    Array.of = _NativeArray.of; 
    Array.isArray = _NativeArray.isArray;

    return { //Methods to expose externally. 
        Array: Array
    };
})(Array);

完整示例:

///Collections functions as a namespace.     
///_NativeArray to prevent naming conflicts.  All references to Array in this closure are to the Array function declared inside.     
var Collections = (function (_NativeArray) {
    //__proto__ is deprecated but Object.xxxPrototypeOf isn't as widely supported. '
    var setProtoOf = (Object.setPrototypeOf || function (ob, proto) { ob.__proto__ = proto; return ob; });
    var getProtoOf = (Object.getPrototypeOf || function (ob) { return ob.__proto__; });        

    function Array() {          
        var arr = new (Function.prototype.bind.apply(_NativeArray, [null].concat([].slice.call(arguments))))();           
        setProtoOf(arr, getProtoOf(this));//For any prototypes defined on this subclass such as 'last'            
        return arr;
    }

    //Restores inherited prototypes of 'arr' that were wiped out by 'setProtoOf(arr, getProtoOf(this))' as well as add static functions.      
    Array.prototype = Object.create(_NativeArray.prototype, { constructor: { value: Array } });
    Array.from = _NativeArray.from; 
    Array.of = _NativeArray.of; 
    Array.isArray = _NativeArray.isArray;

    //Add some convenient properties.  
    Object.defineProperty(Array.prototype, "count", { get: function () { return this.length - 1; } });
    Object.defineProperty(Array.prototype, "last", { get: function () { return this[this.count]; }, set: function (value) { return this[this.count] = value; } });

    //Add some convenient Methods.          
    Array.prototype.insert = function (idx) {
        this.splice.apply(this, [idx, 0].concat(Array.prototype.slice.call(arguments, 1)));
        return this;
    };
    Array.prototype.insertArr = function (idx) {
        idx = Math.min(idx, this.length);
        arguments.length > 1 && this.splice.apply(this, [idx, 0].concat([].pop.call(arguments))) && this.insert.apply(this, arguments);
        return this;
    };
    Array.prototype.removeAt = function (idx) {
        var args = Array.from(arguments);
        for (var i = 0; i < args.length; i++) { this.splice(+args[i], 1); }
        return this;
    };
    Array.prototype.remove = function (items) {
        var args = Array.from(arguments);
        for (var i = 0; i < args.length; i++) {
            var idx = this.indexOf(args[i]);
            while (idx !== -1) {
                this.splice(idx, 1);
                idx = this.indexOf(args[i]);
            }
        }
        return this;
    };

    return { //Methods to expose externally. 
        Array: Array
    };
})(Array);

以下是一些使用示例和测试。

var colarr = new Collections.Array("foo", "bar", "baz", "lorem", "ipsum", "lol", "cat");
var colfrom = Collections.Array.from(colarr.reverse().concat(["yo", "bro", "dog", "rofl", "heyyyy", "pepe"]));
var colmoded = Collections.Array.from(colfrom).insertArr(0, ["tryin", "it", "out"]).insert(0, "Just").insert(4, "seems", 2, "work.").remove('cat','baz','ipsum','lorem','bar','foo');  

colmoded; //["Just", "tryin", "it", "out", "seems", 2, "work.", "lol", "yo", "bro", "dog", "rofl", "heyyyy", "pepe"]

colmoded instanceof Array; //true

答案 4 :(得分:0)

结帐。它适用于支持&#39; __ proto __ &#39;

的所有浏览器。
var getPrototypeOf = Object.getPrototypeOf || function(o){
    return o.__proto__;
};
var setPrototypeOf = Object.setPrototypeOf || function(o, p){
    o.__proto__ = p;
    return o;
};

var CustomArray = function CustomArray() {
    var array;
    var isNew = this instanceof CustomArray;
    var proto = isNew ? getPrototypeOf(this) : CustomArray.prototype;
    switch ( arguments.length ) {
        case 0: array = []; break;
        case 1: array = isNew ? new Array(arguments[0]) : Array(arguments[0]); break;
        case 2: array = [arguments[0], arguments[1]]; break;
        case 3: array = [arguments[0], arguments[1], arguments[2]]; break;
        default: array = new (Array.bind.apply(Array, [null].concat([].slice.call(arguments))));
    }
    return setPrototypeOf(array, proto);
};

CustomArray.prototype = Object.create(Array.prototype, { constructor: { value: CustomArray } });
CustomArray.prototype.append = function(var_args) {
    var_args = this.concat.apply([], arguments);        
    this.push.apply(this, var_args);

    return this;
};
CustomArray.prototype.prepend = function(var_args) {
    var_args = this.concat.apply([], arguments);
    this.unshift.apply(this, var_args);

    return this;
};
["concat", "reverse", "slice", "splice", "sort", "filter", "map"].forEach(function(name) {
    var _Array_func = this[name];
    CustomArray.prototype[name] = function() {
        var result = _Array_func.apply(this, arguments);
        return setPrototypeOf(result, getPrototypeOf(this));
    }
}, Array.prototype);

var array = new CustomArray(1, 2, 3);
console.log(array.length, array[2]);//3, 3
array.length = 2;
console.log(array.length, array[2]);//2, undefined
array[9] = 'qwe';
console.log(array.length, array[9]);//10, 'qwe'
console.log(array+"", array instanceof Array, array instanceof CustomArray);//'1,2,,,,,,,,qwe', true, true

array.append(4);
console.log(array.join(""), array.length);//'12qwe4', 11

答案 5 :(得分:0)

我创建了一个简单的NPM模块来解决这个问题 - inherit-array。它基本上做了以下几点:

function toArraySubClassFactory(ArraySubClass) {
  ArraySubClass.prototype = Object.assign(Object.create(Array.prototype),
                                          ArraySubClass.prototype);

  return function () {
    var arr = [ ];
    arr.__proto__ = ArraySubClass.prototype; 

    ArraySubClass.apply(arr, arguments);

    return arr;
  };
};

编写自己的SubArray类后,可以按如下方式继承Array:

var SubArrayFactory = toArraySubClassFactory(SubArray);

var mySubArrayInstance = SubArrayFactory(/*whatever SubArray constructor takes*/)

答案 6 :(得分:0)

带有自定义构造函数的ES6最小可运行示例

如果您还想重写构造函数,则需要格外小心,因为某些方法将需要旧的构造函数。

使用How can I extend the Array class and keep its implementations中提到的技术,我们可以达到:

fn main() {
    let hex = "0xB85991EE5DA2B557";
    let unsigned_long: u64 = str_to_num_with_hex(hex, 0);
    let signed_long: i64 = str_to_num_with_hex(hex, 0);
    println!("{}", unsigned_long); // prints 13283809028865176919
    println!("{}", signed_long); // prints 0
    println!("{}", unsigned_long as i64); // prints -5162935044844374697
}

Implement Array-like behavior in JavaScript without using Array

处要求获取不带#!/usr/bin/env node const assert = require('assert'); class MyArray extends Array { constructor(nodes, myint) { super(...nodes); this.myint = myint; } static get [Symbol.species]() { return Object.assign(function (...items) { return new MyArray(new Array(...items)) }, MyArray); } inc() { return this.myint + 1; } } const my_array = new MyArray([2, 3, 5], 9); assert(my_array[0] === 2); assert(my_array[1] === 3); assert(my_array[2] === 5); assert(my_array.myint === 9); assert(my_array.inc() === 10); assert(my_array.toString() === '2,3,5'); my_slice = my_array.slice(1, 2); assert(my_slice[0] === 3); assert(my_slice.constructor === MyArray); 的索引符号[]

在Node.js v10.15.1。中进行了测试