1. Functions as NamespacesSometimes we need to declare a lot of variables. Such variables pollute global variables and may conflict with variables declared by others. At this point. The workaround is to put the code in a function and then call the function. So the global variable becomeslocal variables. such as:
function my () { }my (); // don't forget to call
This code defines a single global variable: a function called "my". We can also write this by defining an anonymous function:
(function() { /// Here The first opening parenthesis is required, if not written, the JavaScript parser will // Functions are parsed into function declaration statements, and then parsed into function definition expressions } ()) /// () Here, the function is to call it immediately after the definition of the functions is complete.
2. ClosuresTo
understand one (a book):the execution of a function depends on the scope of the variable. In order to implement this lexical scope, you must reference the current scope chain. function objects can be interconnected by the scope chain, and variables inside the function body can be stored within the scope of the function.This feature is called "Closure" in computer science. Technically, all JavaScript functions are closures: they are objects, and they all relate to the scope chain. When a function is called with a scope chain that the closure points to and the scope chain that defines the function is not the same scope chain, things become very subtle. To
understand two (a book):closures are functions that have access to variables in another function scope. a common way to create closures is to create another function inside a function. Here's a copy of the book's Explanation:
What happens when a function is called? when a function is called, it creates an activity object that executes the environment and scope chain and then initializes the function with parameters. In a scope chain, the active object of an external function is always the second bit. In general, when the function is finished, the local active object (which I understand as a local variable) is destroyed and only the global scope (the variable object of the global execution environment) is saved in memory. The role of a scope chain is to guarantee access to all variables and functions in the execution environment. but in closures the situation is different! An intrinsic function adds the active object of an external function to its scope chain. After an anonymous function is returned from an external function, its scope chain is initialized to the object that contains the external function's active object and global variables. This allows anonymous functions to access all variables defined in the external function. When an external function finishes executing, its active object is not destroyed . Because the scope chain of an anonymous function is still referencing the active object. That is, the scope chain of the external function is destroyed, but his active objects remain in memory. the external function's active object is not destroyed until the anonymous function is destroyed . (Closures this thing a bit of trouble--here dare not give an example to confuse everyone's thinking, even the example of the book I think it is not very good to explain the closure, we can check the information, have their own understanding)
3. Closures and variables:This configuration mechanism of the scope chain leads to a noticeable side effect that the closure can only get the last value of any variable in the containing function. Closures save the entire variable object, not a particular variable.
function A () {var arry=[]; for (var i=0;i<10;i++) {arry[i]=function() {return i; } }return arry;}
The above example is not the same as the result we want. Each is returned 10. Because the active object that wipes the CA () function is kept in the scope chain of each function, they refer to the same function variable i. when the CA () returns, the value of the variable i is 10. We can then force the closure to behave as expected by creating another anonymous function.
function CA () { var arry=New Arry (); for (var i=0;i<10;i++) { arry[i]=function(num) { return function() { return num;}} } return Arry;}
instead of assigning the closure function to an array directly, we define an anonymous function and assign the result of executing the anonymous function immediately to the array.
4.Fuction () constructorfunctions can be defined by the function () constructor:
var f=New fuction ("x", "Y", "return x*y;") ); // is equivalent to var f=function(x, y ) {return x*y}
5.constructor Properties
each JavaScript function automatically has a prototype property. The value of this property is an object that contains the objectThe only one non-enumerable property constructor. The value of the constructor property is a function object.look at the code first:
var f=function () {} // This is a functional object var p=f.prototype; // This is an F-associated prototype object var c=p.constructor; // truec===f // for any function f.prototype.constructor==f
you can see the pre-defined constructor property in the constructor's prototype, which means that the objectUsually inherited constructor refer to their constructors. Because the constructor is the "public identity" of the class, this constructor property provides classes for the object.
var o=New F (); // create an object of Class F o.constructor===f //True,constructor property refers to this class
6. Multiple catch clauses
in JavaScript 1.5, Try/catch can use multiple catch clausessuch as:
Try{ //Ken's going to throw a lot of different types of exceptions. Throw1;}Catch(EifEinstanceofreferenceerror) { //reference errors are handled here}Catch(Eife=== "Quit"){ //handling the thrown string here is the case of "quit"}Catch(Eif typeofe=== "string"){ //cases where other strings are processed}Catch(e) {//handling the remaining exceptions}finally{ //The finally clause executes normally}
JavaScript authoritative design--javascript function (Brief study note 12)