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

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o,flag){ if(flaf === undefined){ 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},1) // true [1,{a:1}].has({a:1}) // false 
bortunac
  • 4.8k
  • 1
  • 36
  • 24