Created
January 4, 2013 15:46
-
-
Save abozhilov/4453585 to your computer and use it in GitHub Desktop.
Node.js: Extending Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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