Javascript - 自执行函数:如果我可以使用非自执行函数创建本地作用域,为什么要使用它们? [英] Javascript - self-executing functions : why to use them if I can create local scope with not self-executing functions?

查看:161
本文介绍了Javascript - 自执行函数:如果我可以使用非自执行函数创建本地作用域,为什么要使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里和其他地方有很多关于自我执行功能的帖子,但在阅读帖子后我还有一些问题。

I know there are a lot of posts here and elsewhere about selfexecuting functions but I still have some questions after reading posts.


  1. <为什么我会为变量分配一个自执行函数?如果看起来他们仍然执行自己。
  1. why would I ever assign a self-executing function to a variable? If seems that they execute themselves anyways.

var myFunc=(function() {
 console.log('Hello World');
})();


  • 我读了很多,使用自执行函数的原因是保持变量私有。如果我有一个不自动执行的功能,我在该功能中定义的所有东西都会是私有的吗?!

  • I read a lot that the reason to use self-executing functions is to keep variables private. If I have a not self-executing function, everything I define inside that function is gonna be private anyways?!

    (function() {
     var name="my Name"
     console.log(name);
    })();
    
    vs.
    
     function() {
     var name="my Name"
     console.log(name);
     };
     //its the same
    


  • 所以我不太明白自动执行函数如何保持局部范围(因为你可以使用非自动执行的函数),所以我看到的唯一原因是当你想自动执行时,例如在页面加载时使用它们。

    So I dont quite understand how self-executing functions are to keep local scope (as you can do that using not self-executing functions) so the only reason I see is to use them when you want to execute automatically for example on page load.

    谢谢!

    还有一个问题:

    var test=(function myFunc(){
          var name="Hello World"
          return {
            test1: function(){
              return name;
            },
            test2:function(){
              return name+"1"
            }
          }
        })()
    
        test.test1()
    
    vs
    
        var test=function myFunc(){
          var name="Hello World"
          return {
            test1: function(){
              return name;
            },
            test2:function(){
              return name+"1"
            }
          }
        }
    
        test.test1()
    

    - >这里究竟发生了什么因为IIFE我实际上可以执行test.test1 ()而不是常规函数?

    --> what exactly happens here that because of IIFE I can actually execute test.test1() and not with a regular function?

    推荐答案

    当你想保留你的范围时,你通常会将你的函数包装在一个匿名函数中遏制。这也是模块模式的一部分,它仍然非常流行:

    You usually wrap your functions in a anonymous function when you want to keep your scope contained. This is also part of the module pattern which is still pretty popular:

    https://toddmotto.com/mastering-the-module-pattern/

    然后你可以分配结果 IIFE 变量,因此您的范围只能是通过调用该变量来访问。

    Then you can assign the outcome of that IIFE to a variable so your scope can only be accessed by calling that variable.

    myScope.myLocallyScopedProperty or myScope[myLocallyScopedProperty]
    

    您的其他功能需要手动调用,也可以从任何地方访问。

    Your other function needs to be called manually and it also accessible from anywhere.

    我建议阅读Todd Moto的文章解释了很多。

    I suggest reading the article by Todd Moto it explains a lot.

    这篇关于Javascript - 自执行函数:如果我可以使用非自执行函数创建本地作用域,为什么要使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆