为什么Function.x在声明Function.prototype.x后工作?

lev*_*evi 3 javascript prototype

据我所知,Function的prototype属性是如何向从该函数实例化的所有对象添加方法/属性.

所以,当我尝试这样的事情

function Person(){}
Person.prototype.saySomething = function(){ alert( "hi there" ); }

Person.saySomething();
Run Code Online (Sandbox Code Playgroud)

我得到错误"Person.saySomething不是一个函数",这是有意义的,因为我没有在Person对象实例上执行该函数.

但为什么运行以下代码工作呢?

Function.prototype.sayHi = function(){ alert( "hi!" );}

Function.sayHi();
Run Code Online (Sandbox Code Playgroud)

Rob*_*b W 5

您必须先创建一个实例Person:

new Person().saySomeThing();
Run Code Online (Sandbox Code Playgroud)

原型方法/属性仅在通过new关键字创建构造函数的实例时继承.

Function.sayHi()有效,因为Function构造函数也是一个函数.