Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created January 4, 2013 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abozhilov/4453585 to your computer and use it in GitHub Desktop.
Save abozhilov/4453585 to your computer and use it in GitHub Desktop.
Node.js: Extending Array
var array = {
new : function () {
var arr = [].slice.call(arguments);
arr.__proto__ = array.proto;
return arr;
},
proto : {
__proto__ : Array.prototype,
last : function () {
return this[this.length - 1];
},
max : function () {
return Math.max.apply(null, this);
},
min : function () {
return Math.min.apply(null, this);
}
}
};
module.exports = array;
//Example
var arr = array.new(1, 2, 3, 4);
arr.push(5);
console.log(arr.length); //5
console.log(arr.last()); //5
console.log(arr.min()); //1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment