Javascript:原型方法错误?
我收到“TestFunc 未定义”错误:
/* my_object.js */
"use strict";
function MyObject (param) {
this.param = param;
}
MyObject.prototype.TestFunc = function () {
console.log ('in TestFunc');
}
MyObject.prototype.RealFunc = function () {
// I have tried 3 different ways to call TestFunc:
// 1.
this.TestFunc ();
// 2.
TestFunc ();
// 3. (I didn't really think this would work,
// but thought it was worth a try...)
MyObject.TestFunc ();
}
当这段代码... ...从这段代码运行时,
/* index.js */
var myObj = new MyObject ('test');
myObj.RealFunc (); // Firebug: "TestFunc is not defined"
I am getting a "TestFunc is not defined" error when this bit of code...
/* my_object.js */
"use strict";
function MyObject (param) {
this.param = param;
}
MyObject.prototype.TestFunc = function () {
console.log ('in TestFunc');
}
MyObject.prototype.RealFunc = function () {
// I have tried 3 different ways to call TestFunc:
// 1.
this.TestFunc ();
// 2.
TestFunc ();
// 3. (I didn't really think this would work,
// but thought it was worth a try...)
MyObject.TestFunc ();
}
...gets run from this bit of code:
/* index.js */
var myObj = new MyObject ('test');
myObj.RealFunc (); // Firebug: "TestFunc is not defined"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没关系。删除其他调用后,这会起作用。
(嗯,只要您不将
RealFunc
从其所有者手中剥离并自行调用它,就像var method= myObj.RealFunc; method();
var method= myObj.RealFunc; method(); 或通过事件处理程序或超时,在这种情况下,RealFunc 中的this
不会是 MyObject 实例,您需要查看闭包或Function.bind
让它工作。)不,TestFunc 没有定义为本地或全局范围内的变量。这会导致您从 Firebug 收到错误。
不,你是对的。 :-) 它将是显式完成的
MyObject.prototype.TestFunc.call(this)
。JavaScript 确实有点混淆了问题,因为它在内置对象的标准构造函数上放置了一些与其原型相同的方法(例如
String.split
存在,而实际上只有String.split )。原型.split 应该)。但这种情况不会发生在您自己的对象上,除非您明确声明类似
MyObject.TextFunc= MyObject.prototype.TextFunc
的内容。That's fine. That'll work, with the other calls removed.
(Well, it works as long as you don't peel off the
RealFunc
from its owner and call it on its own, likevar method= myObj.RealFunc; method();
or via an event handler or timeout. In that casethis
in RealFunc wouldn't be the MyObject instance and you'd need to look at closures orFunction.bind
to get it to work.)Nope, TestFunc is not defined as a variable in local or global scope. This causes the error you get from Firebug.
No, you were right. :-) It would be
MyObject.prototype.TestFunc.call(this)
, done explicitly.JavaScript does kind of confuse the matter by putting some of the same methods on the standard constructor functions for built-in objects as on their prototypes (for example
String.split
exists where really onlyString.prototype.split
should). But that doesn't happen to your own objects unless you explicitly say something likeMyObject.TextFunc= MyObject.prototype.TextFunc
.变体 1 似乎是正确的。我在 ExecuteJS 中尝试了您的代码并跳过了 2. 和 3.,它起作用了(尽管我删除了对
console.log
的调用并将其更改为alert
)。TestFunc
在RealFunc
中调用。如果删除
"use strict";
会发生什么?Variant 1 seems correct. I tried your code in ExecuteJS and skipped 2. and 3., it worked (although I removed the call to
console.log
and changed it toalert
).TestFunc
is called withinRealFunc
.What happens if you remove
"use strict";
?