Javascript TypeError:调用Function.prototype.method()时未定义this.prototype

Javascript TypeError:调用Function.prototype.method()时未定义this.prototype,javascript,function,prototype,Javascript,Function,Prototype,我正在读《Javascript:好的部分》一书。 现在我正在阅读关于扩充类型的一章: Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; 更新: 为什么下面的代码不起作用 js> Function.prototype.method("test", function(){print("TEST")}); typein:2: TypeErr

我正在读《Javascript:好的部分》一书。
现在我正在阅读关于扩充类型的一章:

Function.prototype.method = function (name, func) {
   this.prototype[name] = func;
   return this;
};
更新:
为什么下面的代码不起作用

js> Function.prototype.method("test", function(){print("TEST")});
typein:2: TypeError: this.prototype is undefined
但以下代码可以正常工作:

js> Function.method("test", function(){print("TEST")});
function Function() {[native code]}
为什么这个代码有效

js> var obj = {"status" : "ST"};
js> typeof obj;
"object"
js> obj.method = function(){print(this.status)};
(function () {print(this.status);})
js> obj.method();
ST
“obj”是对象。
但我可以在上面调用方法“method”。
Function.prototype.method和obj.method有什么区别?

因为:

Function instanceof Function           // <--- is true
Function.prototype instanceof Function // <-- is false

prototype仅在声明函数时使用,而不是在调用函数时使用。Prototype使函数成为使用该对象的每个实例创建的对象的成员。

指的是
函数。Prototype
,因为您调用了
.method
。所以,您使用的是不存在的
函数.prototype.prototype


使用
Function.method(…)
this[name]=…
来消除
.prototype
s中的一个。

“调用
Function.prototype.method
时,您调用的是一个普通函数。
指向未定义(严格模式)或
窗口
”—这是不正确的。它指向你调用它的对象;在本例中,
Function.prototype
@pimvdb那么我的问题中的object Function.prototype和object obj之间有什么区别呢?@pimvdb谢谢您的通知。@VladimirBezugliy调用
Function.prototype.method()
,这指向一个没有
prototype
属性的对象。在REPL:
({}).prototype中键入此项。返回值将是未定义的
。谢谢。现在我明白了。我没有注意下面这行。prototype[name]=func;
Function.method()                // is equivalent to
(function Function(){}).method()
(new Function).method()          // Because Function is also a function

Function.prototype.method // No instance, just a plain function call