The Array.prototype
property represents the prototype for the Array
constructor and allows you to add new properties
and methods
to all Array
objects. we can create a prototype for this purpose
Array.prototype.has_element = function(element) {
return $.inArray( element, this) !== -1;
};
And then use it like this
var numbers= [1, 2, 3, 4];
numbers.has_element(3) => true
numbers.has_element(10) => false
See the Demo below
Array.prototype.has_element = function(element) {_x000D_
return $.inArray(element, this) !== -1;_x000D_
};_x000D_
_x000D_
_x000D_
_x000D_
var numbers = [1, 2, 3, 4];_x000D_
console.log(numbers.has_element(3));_x000D_
console.log(numbers.has_element(10));
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
_x000D_