& # 39;方法# 39;在函数.道格拉斯·克罗克福德书中的原型

'method' on Function.prototype in Douglas Crockford's book

本文关键字:原型 格拉斯 方法 函数 道格拉 克罗      更新时间:2023-09-26

在Crockford的书中'method'被添加到Function.prototype

在我的浏览器控制台中,我这样做了

  1. Function.method- - ->定义
  2. Function.prototype.method=function(name,func){this.prototype[name]=func; return this;} --> function(name,func){this.prototype[ame]=func; return this;}
  3. (function(){}).method --> function(name,func){this.prototype[name]=func; return this;}
  4. Function.method --> function(name,func){this.prototype[name]=func; return this;}

我可以理解第3步,其中一个函数文字有'方法'定义

但是第4步,函数是怎么来的?在步骤1中未定义的方法,突然定义了' Method '。

函数是自身的实例吗?

是的。

Function instanceof Function;
> true