Skip to main content
added 88 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24

[ ].has(obj)

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o, flag){ if (flag === undefined) { return this.indexOf(o) !== -1; } else { // only for raw js object 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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

comparing raw objects

[o1].has(o2,true) // true if every level value is same 

[ ].has(obj)

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o, flag){ if (flag === undefined) { return this.indexOf(o) !== -1; } else { // only for raw js object 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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

[ ].has(obj)

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o, flag){ if (flag === undefined) { return this.indexOf(o) !== -1; } else { // only for raw js object 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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

comparing raw objects

[o1].has(o2,true) // true if every level value is same 
added 20 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24

[ ].has(obj)

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o, flag){ if (flag === undefined) { return this.indexOf(o) !== -1; } else { // only for raw js object 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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o, flag){ if (flag === undefined) { return this.indexOf(o) !== -1; } else { // only for raw js object 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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

[ ].has(obj)

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o, flag){ if (flag === undefined) { return this.indexOf(o) !== -1; } else { // only for raw js object 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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

Improved formatting
Source Link
t j
  • 7.3k
  • 12
  • 49
  • 67

assuming .indexOf().indexOf() is implemented

Object.defineProperty( Array.prototype,'has',  {   value:function(o, flag){   if (flag === undefined) {   return this.indexOf(o) !== -1;   }  else { // only for raw js object   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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has',  {   value:function(o,flag){   if(flag === undefined){   return this.indexOf(o) !== -1;   }  else{ // only for raw js object   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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

assuming .indexOf() is implemented

Object.defineProperty( Array.prototype,'has', { value:function(o, flag){ if (flag === undefined) { return this.indexOf(o) !== -1; } else { // only for raw js object 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=function(){... because you'll add an enumerable element in every array 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 

the use of 2nd arg (flag) forces comparation by value instead of reference

added 3 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
added 26 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
added 26 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
added 64 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
added 16 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
added 555 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
deleted 59 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
added 6 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
added 60 characters in body
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading
Source Link
bortunac
  • 4.8k
  • 1
  • 36
  • 24
Loading