Last active
December 24, 2015 17:49
-
-
Save capaj/6838404 to your computer and use it in GitHub Desktop.
helpful array methods- empty and contains, and property last
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.defineProperty(Array.prototype, 'last', { | |
enumerable: false, | |
configurable: true, | |
get: function() { | |
return this[this.length - 1]; | |
}, | |
set: undefined | |
}); | |
/** | |
* check whether array contains supplied item | |
*/ | |
Object.defineProperty(Array.prototype, 'contains', { | |
enumerable: false, | |
configurable: false, | |
/** | |
* @param {*} val | |
* @returns {boolean} | |
*/ | |
value: function (val) { | |
return this.indexOf(val) != -1; | |
}, | |
writable: false | |
}); | |
/** | |
* will erase the array and leave it empty | |
*/ | |
Object.defineProperty(Array.prototype, 'empty', { | |
enumerable: false, | |
configurable: false, | |
value: function () { | |
this.length = 0; | |
}, | |
writable: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment