IIFE 上下文问题

发布于 2024-12-08 07:50:44 字数 786 浏览 1 评论 0原文

在以下构造中:

(function(){

    var x = function(){
        alert('hi!');
    }

    var y = function(){
        alert("hi again!");
    }

    this.show = function(){
        alert("This is show function!");
    }

})();

为什么 this 引用 window 对象? IIFE 内的所有内容都应该与全局范围隔离吗? xy 函数也是 window 全局对象的属性吗?

另外,即使我在开头使用 put var h = ...

var h = (function(){

    var x = function(){
        alert('hi!');
    }

    var y = function(){
        alert("hi again!");
    }

    this.show = function(){
        alert("This is show function!");
    }

})();

this 仍然引用 window 对象 - 我可以只调用 show()< /code> 来自全局范围!怎么会?

In the following construct:

(function(){

    var x = function(){
        alert('hi!');
    }

    var y = function(){
        alert("hi again!");
    }

    this.show = function(){
        alert("This is show function!");
    }

})();

Why does this refer to window object? Should everything inside IIFE be isolated from global scope? Are x and y functions also properties of window global object?

Also, even if I use put var h = ... at the beginning:

var h = (function(){

    var x = function(){
        alert('hi!');
    }

    var y = function(){
        alert("hi again!");
    }

    this.show = function(){
        alert("This is show function!");
    }

})();

this still refers to window object -- I can just call show() from the global scope! How come?

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

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

发布评论

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

评论(2

你好,陌生人 2024-12-15 07:50:44

全局上下文(浏览器中的window)是没有其他值可用时this 获取的值。

您的局部变量是局部的(即不是 window 的属性)。它们在函数内部用 var 声明。

添加 var h = (function(){... 没有区别的原因是调用函数的方式不同。函数引用不是对象的属性值(如 something.func()),并且您不使用 .call().apply() 调用它,因此 this 指的是全局(window)对象就是这样。语言被定义为行动。

The global context (window in a browser) is the value this gets when there's no other value to use.

Your local variables are local (that is, not properties of window). They're declared inside the function with var.

The reason why adding var h = (function(){... makes no difference is because of the way you call the function. The function reference is not a property value of an object (like something.func()), and you don't invoke it with .call() or .apply(), so therefore this refers to the global (window) object. That's just the way the language is defined to act.

月牙弯弯 2024-12-15 07:50:44

@Pointy是正确的,但他没有提出整个问题 - 您可能会对

一般来说,在 IIFE 中不需要 this,因为您可以直接访问匿名函数作用域中定义的任何函数或变量 - show() 可以调用 < code>x() 和 y() 直接,因此不需要 this 引用。可能有一个使用 new 实例化 IIFE 的有效用例,但我从未遇到过。

@Pointy is correct, but he doesn't present the whole issue - you might be interested in this related answer. The issue here is that if you aren't using the new keyword, you aren't instantiating an object, so there's no instance for this to refer to. In the absence of an instance, this refers to the window object.

In general, you don't need this within an IIFE, because you have direct access to any function or variable defined in the anonymous function's scope - show() can call x() and y() directly, so there's no need for a this reference. There may be a valid use case for instantiating an IIFE with new, but I've never come across it.

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