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 > How References to Object Properties Ar...

How References to Object Properties Are Resolved

If you attempt to access a property that is not contained in an object, JavaScript will always attempt to find the property or method using the prototype chain. Below, I create an array and then attempt to access a property called foo that has not yet been defined. You might think that because myArray.foo is not a property of the myArray object, JavaScript will immediately return undefined. But JavaScript will look in two more places (Array.prototype and then Object.prototype) for the value of foo before it returns undefined.

Live Code

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

var myArray = [];

console.log(myArray.foo); // logs undefined

/* JS will look at Array.prototype for Array.prototype.foo, but it is not there. 
Then it will look for it at Object.prototype, but it is not there either, 
so undefined is returned! */

</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 10-Day Trial


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