Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification needed for prototype/object statement and chain traversal [duplicate]

Tags:

javascript

While still struggling to read "You don't know JS", I am start to have good idea(love this series). I think I get the hang of prototype but I ran into below code.

   var myObject = {
     a:2
   };

   Object.getOwnPropertyDescriptor(myObject, "a");

And while I fully comprehend the output and its meaning, I was trying to use my understanding (or lack thereof) of prototype and wanted to do below.

   myObject.getOwnPropertyDescriptor

I thought it would traverse up the proto chain up to Object's prototype and get that method but as it turns out, Object's prototype does not have this (assume this is not part of prototype of object as I am looking up the doc, at least I don't see it as part of prototype and it says it's a method). So instead of being Object.prototype.getOwnPropertyDescriptor, I assume this is just Object.getOwnPropertyDescriptor.

Am I understanding this correctly and what is the reason why Object's method is not on all prototype?

like image 290
user3502374 Avatar asked Oct 19 '22 20:10

user3502374


1 Answers

this is not part of the prototype of Object... it's a method

You're exactly right. This can be immediately verified in a js console:

> Object.getOwnPropertyDescriptor
getOwnPropertyDescriptor() { [native code] }
> Object.prototype.getOwnPropertyDescriptor
undefined

In a more strictly OOP language, you might call getOwnPropertyDescriptor a static method. More properly, it's not part of the prototype chain.

like image 124
Chris Avatar answered Nov 15 '22 02:11

Chris