Javascript Prototypes And Inheritance
My question is Object is last on prototype chain all objects inherit properties and methods from it how it is inherit methods from Function.prototype, why Object.__proto__ === Func
Solution 1:
The fundamental Object
in JavaScript has to have a way for it to be instantiated, so it requires a prototype
object to aid in lines like this:
let myObj = newObject();
And that is why Object.__proto__
is not null
.
The Function
object is a special type of object that facilitates object instantiation via a constructor and when used this way is known as a "constructor function". So, it makes sense for Object
to inherit from a Function
object so that object instances can be made.
Solution 2:
Function is also an object. So it inherits from Object too.
But don't be confused by Object.__proto__
. That's something the browser adds and isn't actually part of the language. So don't rely on it or use it.
Essentially, Function.prototype
is Object.__proto__
.
Post a Comment for "Javascript Prototypes And Inheritance"