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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With