Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:06
Show Gist options
  • Save st98/3725b6571928bc072a3a to your computer and use it in GitHub Desktop.
Save st98/3725b6571928bc072a3a to your computer and use it in GitHub Desktop.
Array#has。
Object.defineProperty(Array.prototype, 'has', {
value: function (value) {
return this.indexOf(value) > -1;
},
enumerable: false,
configurable: true,
writable: false
});
Object.defineProperty(Array.prototype, 'contains',
Object.getOwnPropertyDescriptor(Array.prototype, 'has')
);
//-----
[1, 2, 3].has(2); // => true
[1, 2, 3].has(4); // => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment