10

我是原型设计和实例化的新手,因此有一个问题:

  • 如何创建一个函数来构造一个新数组,该数组还添加了一些带有原型但不修改默认数组函数的属性?

例如 :

function Cool_Object() {
    this = new Array() // Construct new array.
    //This is only for the example. I know you can't do that.
}
Cool_Object.prototype.my_method = function() {
    // Some method added
};

所以,如果你打电话:

var myObject = new Cool_Object();

myObject 将是一个数组,并有一个名为“ my_method”的方法(它实际上调用了一个函数)。

但是默认的 Array 对象是完整的。

提前致谢 !

4

5 回答 5

10

你有点倒退了。只需Array.prototype用作您的自定义对象的原型。

function Cool_Object() {

    this.my_method = function () {
        return 42;
    }
}

Cool_Object.prototype = Array.prototype;

var foo = new Cool_Object();
foo.my_method(); // 42
foo.push(13);
foo[0]; // 13

通过引入中间类型,您可以在不修改 的情况下同时获得和Array.prototype的原型:my_methodCool_ObjectArray.prototype

function Even_Cooler() {}
Even_Cooler.prototype = Array.prototype;

function Cool_Object() {}
Cool_Object.prototype = new Even_Cooler();

Cool_Object.prototype.my_method = function () {
    return 42;
}
于 2013-01-11T00:24:25.223 回答
6

您不能只分配给this,它不起作用并抛出ReferenceError. 只需Cool_Object扩展Array

一种方法:

var Cool_Object = Object.create(Array.prototype);
Cool_Object.my_method = function() {
    // Some method added
};

然后创建更多对象

var obj = Object.create(Cool_Object);
于 2013-01-11T00:26:55.353 回答
1

使用数组作为函数的原型,让你的新类型“继承”自 Array,然后在原型中引入新方法:

function CustomArray() {}

CustomArray.prototype = [];

// introduce a new method to your custom array type
CustomArray.prototype.total = function() {
    return this.reduce(function(ret, el) {
        return ret+el;
    }, 0);
};

// introduce another new method to your custom array type
CustomArray.prototype.arithmetiMean = function() {
    return this.total()/this.length;
};

或者,您可以在新实例中引入这些方法:

function CustomArray() {
    // introduce a new method to your custom array object
    this.total = function() {
        return this.reduce(function(ret, el) {
            return ret+el;
        }, 0);
    };

    // introduce another new method to your custom array object    
    this.arithmetiMean = function() {
        return this.total()/this.length;
    };
}

CustomArray.prototype = [];

var arr = new CustomArray();

arr.push(1); // push is an array-standard method
arr.push(2);
arr.push(3);
arr.push(4);
arr.push(5);
arr.push(6);
arr.push(7);
arr.push(8);
arr.push(9);
arr.push(10);

console.log(arr.arithmetiMean());
于 2013-01-11T00:30:16.503 回答
0

function PseudoArray() {

};
PseudoArray.prototype = Object.defineProperties(Object.create(Array.prototype), {
	constructor: {value:PseudoArray}
})

于 2017-02-12T18:32:42.577 回答
-1

添加这个以供参考,因为现在大多数浏览器都支持 Object.create,所以创建自己的数组对象的好方法是这样的:

function MyCustomArray(){

}

MyCustomArray.prototype = $.extend(Object.create(Array.prototype), {
    /* example of creating own method */
    evenonly : function(){
        return this.filter(function(value){return (value % 2 == 0);});
    },

    /* example for overwriting existing method */
    push : function(value){
        console.log('Quit pushing me around!');

        return Array.prototype.push.call(this, value);
    }
});

var myca = new MyCustomArray();
myca instanceof MyCustomArray /*true*/
myca instanceof Array /*true*/
myca instanceof Object /*true*/
myca.push(1); /*Quit pushing me around!*/
myca.push(2); /*Quit pushing me around!*/
myca.push(3); /*Quit pushing me around!*/
myca.push(4); /*Quit pushing me around!*/
myca.push(5); /*Quit pushing me around!*/
myca.push(6); /*Quit pushing me around!*/

myca.length; /*6*/
myca.evenonly() /*[2, 4, 6]*/

使用 jQuery 的 $.extend,因为它可以方便地保持代码结构化,但不需要它,您可以这样做:

MyCustomArray.prototype = Object.create(Array.prototype);

MyCustomArray.prototype.push = function(){...}

我更喜欢在原型上定义方法,而不是将它们放在构造函数中。它更干净,并且可以避免您的自定义数组对象被不必要的功能弄得一团糟。

于 2016-04-10T13:04:41.627 回答