Javascript:原型方法错误?

发布于 2024-08-20 05:28:00 字数 668 浏览 5 评论 0原文

我收到“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尬尬 2024-08-27 05:28:00
// 1.
this.TestFunc ();

没关系。删除其他调用后,这会起作用。

(嗯,只要您不将 RealFunc 从其所有者手中剥离并自行调用它,就像 var method= myObj.RealFunc; method();var method= myObj.RealFunc; method(); 或通过事件处理程序或超时,在这种情况下,RealFunc 中的 this 不会是 MyObject 实例,您需要查看闭包或 Function.bind让它工作。)

// 2.
TestFunc ();

不,TestFunc 没有定义为本地或全局范围内的变量。这会导致您从 Firebug 收到错误。

// 3. (I didn't really think this would work,
//     but thought it was worth a try...)
MyObject.TestFunc ();

不,你是对的。 :-) 它将是显式完成的MyObject.prototype.TestFunc.call(this)

JavaScript 确实有点混淆了问题,因为它在内置对象的标准构造函数上放置了一些与其原型相同的方法(例如 String.split 存在,而实际上只有 String.split )。原型.split 应该)。但这种情况不会发生在您自己的对象上,除非您明确声明类似 MyObject.TextFunc= MyObject.prototype.TextFunc 的内容。

// 1.
this.TestFunc ();

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, like var method= myObj.RealFunc; method(); or via an event handler or timeout. In that case this in RealFunc wouldn't be the MyObject instance and you'd need to look at closures or Function.bind to get it to work.)

// 2.
TestFunc ();

Nope, TestFunc is not defined as a variable in local or global scope. This causes the error you get from Firebug.

// 3. (I didn't really think this would work,
//     but thought it was worth a try...)
MyObject.TestFunc ();

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 only String.prototype.split should). But that doesn't happen to your own objects unless you explicitly say something like MyObject.TextFunc= MyObject.prototype.TextFunc.

逆夏时光 2024-08-27 05:28:00

变体 1 似乎是正确的。我在 ExecuteJS 中尝试了您的代码并跳过了 2. 和 3.,它起作用了(尽管我删除了对 console.log 的调用并将其更改为 alert)。 TestFuncRealFunc 中调用。

如果删除 "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 to alert). TestFunc is called within RealFunc.

What happens if you remove "use strict";?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文