Skip to main content
5 of 13
added 555 characters in body
bortunac
  • 4.8k
  • 1
  • 36
  • 24

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o){ if(typeof o === "string" || typeof o === "number"){ return this.indexOf(o) !== -1; } else{ // comparing non scalar for(var v in this){ if(JSON.stringify(this[v]) === JSON.stringify(o)) return true; } return false; }, // writable:false, // enumerable:false } ) 

!!! do not make Array.prototype.has because then the method is enumerable and js is broken

//use like [22 ,'a', {prop:'x'}].has(12) // false ["a","b"].has("a") // true [1,{a:1}].has({a:1}) // true 
bortunac
  • 4.8k
  • 1
  • 36
  • 24