Last active
December 5, 2017 11:11
-
-
Save k1r0s/ce9cacfe52e38169bafdc0bb68761691 to your computer and use it in GitHub Desktop.
Add some useful methods to native JavaScript Array
This file contains hidden or 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
| Object.assign(Array.prototype, { | |
| has(val) { | |
| return this.indexOf(val) > -1; | |
| }, | |
| first() { | |
| return this[0]; | |
| }, | |
| last() { | |
| return this[this.length - 1]; | |
| } | |
| }); | |
| const arr = [1, 2, 3]; | |
| console.assert(arr.first() === 1, arr); | |
| console.assert(arr.last() === 3, arr); | |
| console.assert(arr.has(4) === false, arr); | |
| console.assert(arr.has(3) === true, arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment