javascript 类型错误:调用 Function.prototype.method() 时 this.prototype 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9199344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:55:07  来源:igfitidea点击:

TypeError: this.prototype is undefined when calling Function.prototype.method()

javascriptfunctionprototype

提问by Volodymyr Bezuglyy

I am reading the book "Javascript: The good parts".
Now I am reading chapter about Augmenting Types:

我正在阅读“Javascript:好的部分”一书。
现在我正在阅读关于增强类型的章节:

Function.prototype.method = function (name, func) {
   this.prototype[name] = func;
   return this;
};

UPDATE:
Why following code does not work?

更新:
为什么下面的代码不起作用?

js> Function.prototype.method("test", function(){print("TEST")});
typein:2: TypeError: this.prototype is undefined

But following code works without problems:

但是以下代码可以正常工作:

js> Function.method("test", function(){print("TEST")});
function Function() {[native code]}

Why this code works?

为什么这段代码有效?

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" is object.
But I can call method "method" on it.
What is the difference between Function.prototype.method and obj.method?

“obj”是对象。
但是我可以在它上面调用方法“方法”。
Function.prototype.method 和 obj.method 有什么区别?

采纳答案by pimvdb

thisrefers to Function.prototypebecause you called .methodon that. So, you're using Function.prototype.prototypewhich does not exist.

this指的是Function.prototype因为你调用.method了那个。所以,你使用的Function.prototype.prototype是不存在的。

Either use Function.method(...)or this[name] = ...to eliminate one of the .prototypes.

使用Function.method(...)this[name] = ...消除.prototypes 之一。

回答by Rob W

Because:

因为:

Function instanceof Function           // <--- is true
Function.prototype instanceof Function // <-- is false
  • Function.prototypeis an Object, and does not inherit anything from the Function contructor.
  • Functionis a constructor, but also a function, so it inherits methods from Function.prototype.

  • When calling Function.method, you're calling the methodmethod of an instance of Function. So, thispoints to the created instance of Function.
  • When calling Function.prototype.method, you're invoking an ordinary method of an object. thispoints to Function.prototype.
  • Function.prototype是一个对象,不从函数构造函数继承任何东西。
  • Function是一个构造函数,也是一个函数,所以它从Function.prototype.

  • 调用 时Function.method,您正在调用 的method实例的方法Function。因此,this指向创建的Function.
  • 调用 时Function.prototype.method,您正在调用对象的普通方法。this指向Function.prototype.

To clarify, here's an example:

为了澄清,这里有一个例子:

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

回答by Kory Hodgson

prototype is only used when you are declaring the function, but not when you are calling it. Prototype makes the function a member of the object that gets created with every instance of that object.

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