As I understand it, a Function’s prototype property is how you add methods/properties to all objects instantiated from that function.
So when I try something like this
function Person(){}
Person.prototype.saySomething = function(){ alert( "hi there" ); }
Person.saySomething();
I get the error “Person.saySomething is not a function”, which makes sense as Im not executing the function on a Person object instance.
But why than does running the below code work?
Function.prototype.sayHi = function(){ alert( "hi!" );}
Function.sayHi();
You have to create an instance of
Personfirst:Prototype methods/properties are only inherit when an instance of the constructor is created, via the
newkeyword.Function.sayHi()works, because theFunctionconstructor is also a function.