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.