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
TypeError: this.prototype is undefined when calling Function.prototype.method()
提问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
this
refers to Function.prototype
because you called .method
on that. So, you're using Function.prototype.prototype
which does not exist.
this
指的是Function.prototype
因为你调用.method
了那个。所以,你使用的Function.prototype.prototype
是不存在的。
Either use Function.method(...)
or this[name] = ...
to eliminate one of the .prototype
s.
使用Function.method(...)
或this[name] = ...
消除.prototype
s 之一。
回答by Rob W
Because:
因为:
Function instanceof Function // <--- is true
Function.prototype instanceof Function // <-- is false
Function.prototype
is an Object, and does not inherit anything from the Function contructor.Function
is a constructor, but also a function, so it inherits methods fromFunction.prototype
.- When calling
Function.method
, you're calling themethod
method of an instance ofFunction
. So,this
points to the created instance ofFunction
. - When calling
Function.prototype.method
, you're invoking an ordinary method of an object.this
points toFunction.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 使函数成为使用该对象的每个实例创建的对象的成员。