Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

2. Working with Objects and Properties > Using hasOwnProperty, Verify That an O...

Using hasOwnProperty, Verify That an Object Property Is Not From the Prototype Chain

While the in operator can check for properties of an object, including properties from the prototype chain, the hasOwnProperty method can check an object for a property that is not from the prototype chain.

Below, we want to know if myObject contains the property foo, and that it is not inheriting the property from the prototype chain. To do this, we ask if myObject has its own property called foo.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myObject = {foo: 'value'};

console.log(myObject.hasOwnProperty('foo')) // logs true

// vs. a property from the prototype chain
console.log(myObject.hasOwnProperty('toString'); // logs false

</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint