立即自执行函数和“this”

发布于 2024-11-09 17:37:20 字数 729 浏览 0 评论 0原文

我想创建一个 javascript 库,所以我认为使其成为一个立即自动执行的函数将是一件很好的事情,可以确保范围安全和一切。

但现在我遇到了使用“this”关键字的问题,我不太明白。

我怎样才能使这样的代码正确工作?目前,它告诉我“图像”未定义。

(function() {

    function lib() {
        this.image = document.getElementById("image");
        this.parts = [
            {name: "part1", num: "1"}
        ];

        this.init = function() {
            $(parts).each(function() {
                var partNum = this.num;
                image.getElementById(partNum).addEventListener("click", function() {
                    toggleHighlight(partNum);
                }, true);
            });
        };
    }

    window.lib = new lib();
})();

window.lib.init();

如何访问 image 属性?

I want to create a javascript library, so I thought making it an immediately self executing function would be a nice thing to do to ensure scope safety and everything.

But now I'm running into a problem with using the "this" keyword that I don't quite understand.

How can I make a code like this work correctly? Currently, it tells me that "image" is undefined.

(function() {

    function lib() {
        this.image = document.getElementById("image");
        this.parts = [
            {name: "part1", num: "1"}
        ];

        this.init = function() {
            $(parts).each(function() {
                var partNum = this.num;
                image.getElementById(partNum).addEventListener("click", function() {
                    toggleHighlight(partNum);
                }, true);
            });
        };
    }

    window.lib = new lib();
})();

window.lib.init();

How can I access the image property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

甜扑 2024-11-16 17:37:20

我认为您在这里遇到了一些问题:

  • 您在 init 函数中引用 parts ,而没有将其作为 this 的属性进行访问。
  • each 块中,您将 image 作为全局变量进行访问,但它并未全局声明。您无法从该块中的 this 访问 image,因为在该上下文中,this 实际上设置为您正在迭代的项目。 (这就是为什么您可以访问 this.num。)

我建议您包含 var that = this;在你的 init 函数之前,并在访问诸如 that.image 之类的属性时使用它。

var that = this;
this.init = function() {
    $(this.parts).each(function() {
        var partNum = this.num;
        that.image.getElementById(partNum).addEventListener("click", function() {
            toggleHighlight(partNum);
        }, true);
    });
};

I think you have a few issues here:

  • you are referencing parts in your init function without accessing it as a property of this.
  • within the each block, you are accessing image as a global variable but it is not declared globally. You can't access image from this in that block because in that context, this is actually set to the item you are iterating on. (That is why you can access this.num.)

I sugggest you include var that = this; before your init function and use it when accessing properties like that.image.

var that = this;
this.init = function() {
    $(this.parts).each(function() {
        var partNum = this.num;
        that.image.getElementById(partNum).addEventListener("click", function() {
            toggleHighlight(partNum);
        }, true);
    });
};
乖乖 2024-11-16 17:37:20

如果我理解正确,您希望从您提供给 each 的匿名函数访问在 lib 函数内部定义的 this.image 属性>。为此,您需要保留 this 以使其在匿​​名函数中可用:

this.init = function() {
    var self = this;
    $(parts).each(function() {
        //do something with self.image
        self.image;
    });
};

If I understand you right you want to get access to this.image property defined inside of lib function from the anonymous function which you provide to the each. To do this you need to preserve this to make it available in the anonymous function:

this.init = function() {
    var self = this;
    $(parts).each(function() {
        //do something with self.image
        self.image;
    });
};
握住你手 2024-11-16 17:37:20

您还应该将函数名称 lib 的第一个字母大写,因为您希望将其用作具有 new 关键字的构造函数。看看《Javascript The Good Parts》的作者对 JavaScript 中的构造函数命名约定。

You should also capitalize the first letter of function name lib since you are expecting it to be used as a constructor with the new keyword. Take a look at what the author of Javascript The Good Parts has to say about constructor naming conventions in JavaScript.

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