Closing javascript and placing functions

Does function placement affect closure performance within scope? If so, where is the optimal place to place these functions? If not, is the implied association of a closure reason enough to place the function elsewhere logically?

For example, if foo does not rely on the value of localState , whether localState is available from foo has consequences for foo runtime, memory usage, etc.

(function(){
    var localState;

    function foo(){
        // code
    }

    function bar(){
        // code
        return localState;
    }
})();

In other words, would that be the best choice, and if so, why?

(function(){
    function foo(){
        // code
    }

    var localState;

    function bar(){
        // code
        return localState;
    }
})();

Darius Bacon , , localState . , foo , . ?

function foo(){
    // code
}

(function(){

    var localState;

    function bar(){
        // code
        foo();
        return localState;
    }
})();
+3
6

Javascript . , . , y x, x y:

var x = 3;
function y() eval("x");
y();
3
+3

, ( ) , . , localState foo .

... , , foo, , , . , , foo in, foo , foo , , .

+5

, , , JavaScript . , , , .

+5

, , java script . . . , , , !

+2

var - , , , ; , .

, , foo() " " var localState " . , " function foo()" ( , ); Javascript.

+1

. foo , .

, , , , , .

:

CheckOne();
function CheckOne() {
    alert('check...check one.');
}

CheckTwo();
var CheckTwo = function() {
    alert('check...check two.');
};

The only difference between the second and the first is the style that they use to declare their functions. The second generates a reference error.

Greetings.

0
source

Source: https://habr.com/ru/post/1698812/


All Articles