js ............................... Scope

A: Scope and closures

/*
Influenced by the position of the bar function declaration, foo () in the local scope is not recovered after execution (releasing the memory);
The reason is that the function itself is still in use bar, bar () still holds a reference to the scope of, and this is called a reference to closure. That bar () references to foo () the scope.
 */

// Code a: 
/ *
function foo() {
There is 2;
function bar () {// At this bar has not been defined, while external calls he does not exist.
console.log ( 'foo function of local variables:', a);
}
return bar; // return address of the function
}
There s = foo ();
console.log(s);
console.log ( 'perform the function bar');
console.log('\t');
s();
 * / 
// Code 2: 
/ *
var fn; // global variables
function test() {
There is 2;
function bar() {
console.log ( 'perform the function bar ()')
}
bases (bar);
return bar;
}
function baz(s) {
    the console.log ( 'bar in the function call baz () function');
    s();
}
fn = test();
 * / 
// the setTimeout () method of the window object is built, is for the window object may not use the dot operator when using its methods. 
Function the wait (Message) {
the setTimeout ( function Timer () {   // the setTimeout (): function or set calculation expression executed after a specified number of milliseconds. 
the console.log (Message);
},10000)
}
wait('hello,cloure!');
function setupBot(name,selector) {
$(selector).click(function activator() {
console.log('Activating:'+name);
});
}
setupBot('Closure Bot 1','#bot 1');
setupBot('Closure Bot 2','bot 2');

 II: by: defining anonymous function, and the function returns to understand the scope of the function.

  1: Note that the function name:

<!DOCTYPE html>
<html>
<head>
    <Title> function </ title>
</head>
<script type="text/javascript">
    var C = function () / * referenced anonymous function with C * /
    {
        document.write ( "anonymous function execution" );
         var S = Test (); / * S is a partial scope * / 
        S ( 100,50); / * perform a function test_son * / 
        the console.log ( typeof (test_son )); // undefined, Why is undefined, because he has been quoted s. 
        the console.log ( typeof (S)); // function 
        return 0 ;
    }
    function Test () / * function returns a function * /
    {
        var B = 23 is; / * local scope test variables * / 
        function test_son (x, y) / * NOTE Do not use this functiontest_son (var x, var y) thus defined variables described function parameters x, y are variable scope of this block. * /
        {
            console.log("形参x,y="+x+","+y);
            console.log("b");
        }
        return test_son; / * returns the address of the function * /
    }
    c();
</script>
<body>

</body>
</html>

     2: The method is encapsulated into the way the immediate implementation of the anonymous function, if you do not add a return value, less than the external access is added after the return value, global by "anonymous function function name ()." The call. And can hide some private properties and elements, functions and variables private space will not affect the global scope, seen in this way is the best way. Most third-party libraries to use this form, such as jQuery.

<!DOCTYPE html>
<html>
<head>
    <Title> scope </ title>
</head>
<script type="text/javascript">
    var bigfun = (function()/**/
    {
        var big = 12;
        function add(x,y)
        {
            var A = 12 is; / * change amount of the scope: * / 
            the console.log ( "bigfun function variables" + Big);
             return X + Y; / * return value of * /
        }
        function sum()
        {
            document.write ( "SUM function returns no value" );
             return 0 ;
        }
        function divide(a)
        {
            document.write(a);
            document.write ( "divide function execution, outputs Big var" + Big);
             return 0 ;
        }
        return { / * return * / 
            the Add: the Add, // is the object properties bigfun the left, to the right as a function of the address. This is the reference function with the attribute. 
            sum: sum,
            divided: divide
        }
    })();
var X = bigfun.add (100,100); // Output: bigfun variable function 12 is 
the console.log (X); // Output: 200 is 
the console.log ( typeof (bigfun)); // Output: Object 
// bigfun What type of data.?

// define an array?
</script>
<body>

</body>
</html>

 

Guess you like

Origin www.cnblogs.com/1314bjwg/p/12389695.html