Wednesday, September 11, 2013

JavaScript - references to object properties

if the property of an object which you try to access is not available, JavaScript will try searching the prototype chain. At first steps it goes after the constructor function that created the object to check its prototype (e.g., Array.prototype) and if that's not available there it keeps searching up the chain at the constructor behind the initial constructor.

As all those prototype properties are objects, the final step would be Object.prototype because there is no other constructor prototype property upper than Object level to be examined.

Anyway if finally JavaScript can't find the property it would have produced an error stating that the property was undefined.

<!DOCTYPE html><html lang="en"> <body> <script> var myArray = ['element1', 'element2']; console.log(myArray.hasOwnProperty('join')); console.log(myArray.join());
console.log(myArray.hasOwnProperty('toLocaleString')); console.log('toLocaleString' in myArray); console.log(myArray.toLocaleString());
</script> </body> </html>

Share/Bookmark

No comments: