Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个JavaScript代码有什么不同吗?_Javascript_Angularjs - Fatal编程技术网

这个JavaScript代码有什么不同吗?

这个JavaScript代码有什么不同吗?,javascript,angularjs,Javascript,Angularjs,我已经看到了以下两种方式实现的各种代码。我总是用第二个。我想知道两件事 这两种代码有什么区别吗 哪一个是最佳实践(为什么)如果有的话 一, 二, 请也建议我一些好的博客或书籍在这方面,因为我想了解更多的细节。提前谢谢大家 是的,您没有执行第二个 在第一个示例中,您声明了一个匿名函数,该函数在运行之后不带任何参数 在第二个示例中,您只是声明它,而不是运行它 ()是使它运行的原因,在本例中,它不传递任何参数 这个函数执行匿名函数: (function () { //some

我已经看到了以下两种方式实现的各种代码。我总是用第二个。我想知道两件事

  • 这两种代码有什么区别吗

  • 哪一个是最佳实践(为什么)如果有的话

  • 一,

    二,


    请也建议我一些好的博客或书籍在这方面,因为我想了解更多的细节。提前谢谢大家

    是的,您没有执行第二个

    在第一个示例中,您声明了一个匿名函数,该函数在运行之后不带任何参数

    在第二个示例中,您只是声明它,而不是运行它

    ()
    是使它运行的原因,在本例中,它不传递任何参数

    这个函数执行匿名函数:

        (function () {
            //some code here, angular code
        })();
    
    这个不能执行它:

        (function () {
            //some code here, angular code
        });
    
        (function () {
            //some code here, angular code
        });
    
    例如,如果您有一个参数,可以这样传递:

        (function (c) {
            // c.log() is same as console.log
            c.log("hello");
        })(console);
    
    注意:我添加了参数示例,因为没有任何参数它就不那么明显了

    编辑:

    正如@Osman刚刚在评论中指出的,第一个被称为

    这种模式是如此普遍,几年前社区就达成了一致 它的术语:IIFE,代表立即调用的函数 表情


    第二个是声明,第一个是声明和执行。

    第一个是IIFE(立即调用的函数表达式),正如其他人所说,有关IIFE检查的更多信息,请参见Kyle Simpson(《你不知道JS》的作者)

    区别: 第一个执行匿名函数表达式:

    第二个没有执行它:

        (function () {
            //some code here, angular code
        });
    
        (function () {
            //some code here, angular code
        });
    
    匿名函数
    是一个没有名称的函数

    背景: 基本上,首先我们用第一个括号包装匿名函数声明,如:
    ()
    ,使其成为函数表达式

        // this is a function declaration:
        function () {
            //some code here
        }
    
        // this is a function expression
        (function () {
            //some code here
        });
    
    它本身什么也不做,因为我们既不执行它,也不在当前范围内声明它。换句话说,它是无用的。了解更多有关

    现在,我们可以使用函数表达式作为其他函数的参数,就像jQuery那样:

        // now jQuery is taking the function expression as parameter
        $(function () {
            //some code here
        });
    
    或者,我们可以在最后使用
    ()
    来执行函数本身(这就是我们实际调用任何函数的方式-在本例中没有任何参数):

    这也被称为

    上述示例没有任何参数。但是,如果您有一个参数,则可以在执行时像这样传递它:

        (function (c) {
            // Here c.log() is same as console.log()
            c.log("hello");
        })(console);
    
    注意:我添加了参数示例,因为如果没有任何参数,它可能不太明显

    最佳做法: 因为它们在功能上是不同的,所以最佳实践的问题并没有出现。通常,当我们希望在不同于当前范围的范围内执行某些操作时,我们使用IIFE&我们不希望在当前范围内留下任何函数声明、变量声明等的痕迹

    进一步阅读: 有关这方面的更多讨论,请参见以下链接:


  • #2.我从来没打过电话我真的不知道这件事,伙计。你知道有哪个帖子对这个概念有详细的解释吗?我想了解更多
        // Now the function expression gets executed.
        (function () {
            //some code here, angular code
        })();
    
        (function (c) {
            // Here c.log() is same as console.log()
            c.log("hello");
        })(console);