简体   繁体   English

您是否应该使用局部内部函数?

[英]Should you ever use local inner functions?

Which of these is normally seen as better when writing JavaScript. 通常,在编写JavaScript时,哪种方法更好。 Foo, bar, and baz are specific to this function so won't be used anywhere else in other functions. Foo,bar和baz特定于此功能,因此不会在其他功能的其他任何地方使用。

function() {
    foo();
    bar();
    baz();
}

function foo() {
    //lines of code
}

function bar() {
    //lines of code
}

function baz() {
    //lines of code
}

Or 要么

function() {
    function foo() {
        //lines of code
    }
    foo();
    function bar() {
        //lines of code
    }
    bar();
    function baz() {
        //lines of code
    }
    baz();
}

The advantages of the first is it is more efficient since if you are calling the function more that once the other functions only need to be created once. 第一种方法的优点是效率更高,因为如果您多次调用该函数,则一次创建其他函数只需一次。 It is also easier to read as the main outer function is now only 3 lines long and not much longer. 它也更容易阅读,因为主要的外部函数现在只有3行,而且不再长。

The only advantage I can think for the second is it keeps these functions private to the outer function and not accessible to the rest of the file which is good since these functions will not be used anywhere else. 我可以想到的第二个唯一优点是,这些功能使外部函数私有,并且文件的其余部分无法访问这些函数,这很好,因为这些函数不会在其他任何地方使用。 Obviously normally making things public when they shouldn't need to be is bad but is that less important in JavaScript situations like this. 显然,通常情况下,当不需要将其公开时,将它们公开是不好的,但是在这样的JavaScript情况下,它的重要性不那么重要。

Is it possible you would choose differently depending on how often the function was called or anything else? 您是否可以根据函数被调用的频率或其他任何方式进行不同选择?

  • If you want all functions to be publicly available by themselves, declare them separately 如果您希望所有功能可以自己公开使用,请分别声明它们

     function foo() {} // Public, created once function bar() {} // Public, created once function baz() {} // Public, created once var fooBarBaz = function() { // Public, created once foo(); bar(); baz(); }; 
  • If you want the auxiliary functions to be able to access the scope of the main function, with the trade-of that they will have to be recreated each time you call the main function, place the auxiliary function inside the main one 如果您希望辅助功能能够访问主功能的范围,并且要折衷考虑每次调用主功能时都必须重新创建它们,那么请将辅助功能放在主功能中

     var fooBarBaz = function() { // Public, created once function foo() {} // Private, created at each fooBarBaz call function bar() {} // Private, created at each fooBarBaz call function baz() {} // Private, created at each fooBarBaz call foo(); bar(); baz(); }; 
  • If you want to keep the auxiliary functions private, and you don't want to recreate them each time you call the main function, use an IIFE 如果要使辅助功能保持私有,并且不想在每次调用主功能时都重新创建它们,请使用IIFE

     var fooBarBaz = (function() { // IIFE function foo() {} // Private, created once function bar() {} // Private, created once function baz() {} // Private, created once return function() { // Public, created once foo(); bar(); baz(); }; })(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM