Skip to content

Instantly share code, notes, and snippets.

@capaj
Last active December 24, 2015 17:49
Show Gist options
  • Save capaj/6838404 to your computer and use it in GitHub Desktop.
Save capaj/6838404 to your computer and use it in GitHub Desktop.
helpful array methods- empty and contains, and property last
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