I am reading through the official docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
and in the chapter Different ways to create objects and the resulting prototype chain
-> With a constructor
they have the following:
function Graph() {
this.vertices = [];
this.edges = [];
}
Graph.prototype = {
addVertex: function(v) {
this.vertices.push(v);
}
};
var g = new Graph();
// g is an object with own properties 'vertices' and 'edges'.
// g.[[Prototype]] is the value of Graph.prototype when new Graph() is executed.
But right before that it is written that:
// Functions inherit from Function.prototype
// (which has methods call, bind, etc.)
// f ---> Function.prototype ---> Object.prototype ---> null
So, Graph
is a function and it should have Graph.prototype
, in my understanding, to be of type Function.prototype
and NOT
Object.prototype
, otherwise Graph
would stop being a function and would instead become an object, or so I would think.
Could anyone explain why the prototype
of Graph
is assigned to an object and why the specified behavior of inheriting then addVertex
becomes possible? Would we lose all of the function properties (call
, bind
) when such assignment happens?