How to modify Array.prototype in Javascript - Stack Overflow

I am trying to modify Javascripts Array type with a method which will push a value to an array only if

I am trying to modify Javascripts Array type with a method which will push a value to an array only if its is not already present.

Here is my code:

// add a method conditionally
Array.prototype.method = function (name, func){
    if(!this.prototype[name]){
        this.prototype[name] = func;
        return this;
    }
};

// exclusive push
Array.method('xadd', function(value){
    if(this.indexOf(value) === -1){
        this.push(value)
    };
    return this;
});

However when I run the code the scratchpad in Firefox returns:

/*
Exception: TypeError: Array.method is not a function
@Scratchpad/3:19:1
*/

I want a vanilla way of doing this. Not a library as I am writing an open source library.

I am trying to modify Javascripts Array type with a method which will push a value to an array only if its is not already present.

Here is my code:

// add a method conditionally
Array.prototype.method = function (name, func){
    if(!this.prototype[name]){
        this.prototype[name] = func;
        return this;
    }
};

// exclusive push
Array.method('xadd', function(value){
    if(this.indexOf(value) === -1){
        this.push(value)
    };
    return this;
});

However when I run the code the scratchpad in Firefox returns:

/*
Exception: TypeError: Array.method is not a function
@Scratchpad/3:19:1
*/

I want a vanilla way of doing this. Not a library as I am writing an open source library.

Share Improve this question edited Jun 20, 2015 at 22:08 timebandit asked Jun 20, 2015 at 18:52 timebandittimebandit 8383 gold badges12 silver badges27 bronze badges 3
  • Try [].method('xadd',... – thefourtheye Commented Jun 20, 2015 at 18:55
  • 2 method is a method of the Array.prototype object. Instances of the Array object and the prototype have that method. – Ram Commented Jun 20, 2015 at 18:57
  • 2 It's Function.prototype.method! – Bergi Commented Jun 20, 2015 at 20:25
Add a ment  | 

3 Answers 3

Reset to default 3

When you're putting a method on Array.prototype the method will be available on the instances of Array.

// Add the custom method
Array.prototype.method = function() {
    console.log('XXX');
}

var foo = [];
// prints XXX
foo.method();

First I would run a check to see if the method is already on the array. Don't go overridding existing prototype methods. In addition, you're not adding func to the prototype - you're adding it to the instances you'll be creating.

if (!('method' in Array.prototype)) {
    Array.prototype.method = function (name, func) {
        if (!this[name]) this[name] = func;
    }
}

Then you need to actually create your array instance:

var arr = [1,2];

At which point you can use the method you created to add the function. Note in your question your check was incorrect:

arr.method('xadd', function (value) {
    if (this.indexOf(value) === -1) {
        this.push(value)
    };
});

arr.xadd(3); // [1,2,3]

DEMO

Borrowing from Andy & Nihey I have arrived at the following solution which modifies the Array type making 'xadd' conditionally available to all instances of Array

if (!('xpush' in Array.prototype)) {
  Array.prototype.xpush = function(value){
    if(this.indexOf(value) === -1){
      this.push(value);
    };
    return this
  };
}

var a = [1,2,3];
console.log(a); // Array [ 1, 2, 3 ]
a.xadd(5);
console.log(a); // Array [ 1, 2, 3, 5 ]
a.xadd(3);
console.log(a); // Array [ 1, 2, 3, 5 ] '3' already present so not added

A better name would be xpush() as it's behaviour is a variant of push().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790136a4593865.html

相关推荐

  • How to modify Array.prototype in Javascript - Stack Overflow

    I am trying to modify Javascripts Array type with a method which will push a value to an array only if

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信