Skip to content

Instantly share code, notes, and snippets.

@k1r0s
Last active December 5, 2017 11:11
Show Gist options
  • Select an option

  • Save k1r0s/ce9cacfe52e38169bafdc0bb68761691 to your computer and use it in GitHub Desktop.

Select an option

Save k1r0s/ce9cacfe52e38169bafdc0bb68761691 to your computer and use it in GitHub Desktop.
Add some useful methods to native JavaScript Array
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