Search code examples
javascriptobjecthasownproperty

is there a method equal to hasOwnProperty() that can traverse prototype chain?


for example i have an array , lets call it myArray where :

 var myArray = ['foo', 'bar']; 

even though , myArray.join() will return 'foo,bar' , the check myArray.hasOwnProperty('join') , will return false , because simply hasOwnProperty() will not traverse the prototype chain.

Is there a way to do same function with ability to traverse the prototype chain?

P.S : even a custom method will do.


Solution

  • You can use the in operator.

    The in operator returns true for properties in the prototype chain.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

    Like:

    if ('join' in myArray) {
      ...
    }