在javascript中扩展Array对象的方法 [英] Ways to extend Array object in javascript

查看:125
本文介绍了在javascript中扩展Array对象的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用一些用户友好的方法(如Array.Add()而不是Array.push()等)扩展javascript中的Array对象。

i try to extend Array object in javascript with some user friendly methods like Array.Add() instead Array.push() etc...

方法来做到这一点。
unfortunetly第三种方式是不工作,我想问为什么?

i implement 3 ways to do this. unfortunetly the 3rd way is not working and i want to ask why? and how to do it work.

//------------- 1st way
Array.prototype.Add=function(element){
     this.push(element);
};

var list1 = new Array();
list1.Add("Hello world");
alert(list1[0]);

//------------- 2nd way
function Array2 () {
    //some other properties and methods
};

Array2.prototype = new Array;
Array2.prototype.Add = function(element){
  this.push(element);  
};

var list2 = new Array2;
list2.Add(123);
alert(list2[0]);

//------------- 3rd way
function Array3 () {
    this.prototype = new Array;
    this.Add = function(element){
      this.push(element);  
    };
};

var list3 = new Array3;
list3.Add(456);  //push is not a function
alert(list3[0]); // undefined

在第三种方式我想在内部扩展Array对象Array3类。

in 3rd way i want to extend the Array object internally Array3 class. How to do this so not to get "push is not a function" and "undefined"?

这里我添加第四种方式。

Here i add a 4th way.

//------------- 4th way
function Array4 () {
    //some other properties and methods
    this.Add = function(element){
        this.push(element);
    };
 };
Array4.prototype = new Array();

var list4 = new Array4();
list4.Add(789);
alert(list4[0]);

这里我必须使用原型。
我希望避免在类构造函数之外使用额外的线作为Array4.prototype。
我想有一个紧凑的定义类,所有的部分在一个地方。
但我认为我不能这样做。

Here again i have to use prototype. I hoped to avoid to use extra lines outside class constructor as Array4.prototype. I wanted to have a compact defined class with all pieces in one place. But i think i cant do it otherwise.

推荐答案

方法名称应该小写。原型不应在构造函数中修改。

Method names should be lowercase. Prototype should not be modified in the constructor.

function Array3() { };
Array3.prototype = new Array;
Array3.prototype.add = Array3.prototype.push



in CoffeeScript

class Array3 extends Array
   add: (item)->
     @push(item) 

如果你不喜欢这种语法,从构造函数中扩展它
你唯一的选择是:

If you don't like that syntax, and you HAVE to extend it from within the constructor, Your only option is:

// define this once somewhere
// you can also change this to accept multiple arguments 
function extend(x, y){
    for(var key in y) {
        if (y.hasOwnProperty(key)) {
            x[key] = y[key];
        }
    }
    return x;
}


function Array3() { 
   extend(this, Array.prototype);
   extend(this, {
      Add: function(item) {
        return this.push(item)
      }

   });
};

您也可以这样做

ArrayExtenstions = {
   Add: function() {

   }
}
extend(ArrayExtenstions, Array.prototype);



function Array3() { }
Array3.prototype = ArrayExtenstions;

在以前的日子里,prototype.js过去有一个Class.create方法。你可以包装所有这是一个像这样的方法

In olden days, 'prototype.js' used to have a Class.create method. You could wrap all this is a method like that

var Array3 = Class.create(Array, {
    construct: function() {

    },    
    Add: function() {

    }
});

有关此及其实现的更多信息,请参阅prototype.js源代码

For more info on this and how to implement, look in the prototype.js source code

这篇关于在javascript中扩展Array对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆