Function
function declaration Promotion, the function declaration is read before executing the code
sayhi (); function Sayhi () { alert ("hi! " );}
Recursive
Arguments.callee is a pointer to the function being executed
You can also achieve the same effect in a different way
var factorial = (function f (num) { if1) { return1 ; Else { return num * F (num-1); }});
Scope Chain for function execution
function Compare (value1, value2) { if (value1 < value2) { return -1 ; Else if (value1 > value2) { return1; Else { return0; }} var result = Compare (5);
The Compare () function is defined first, and it is called at the global scope. When called, a live object that contains arguments, value1, value2 is created. Variable objects for the global execution environment (containing result and compare) are second in the scope of the Compare () execution Environment
A scope chain is essentially a list of pointers to variable objects, referencing but not actually containing variable objects
Closed Package
Closures are functions that have access to variables in another function scope, a common way to create closures, and create another function inside one function
function Createcomparisonfunction (PropertyName) {returnfunction (Object1, object2) {varValue1 =Object1[propertyname]; varvalue2 =Object2[propertyname]; if(Value1 <value2) { return-1; } Else if(Value1 >value2) { return 1; } Else { return 0; } };}
var compare = Createcomparisonfunction ("name"); var " Nicholas " " Greg " });
After the anonymous function is returned from Createcomparisonfunction (), its scope chain is initialized to the active object and the global variable object that contains the Createcomparisonfunction () function. In this way, the anonymous function can access all the variables defined in Createcomparisonfunction (). Createcomparisonfunction ()
Once the function is executed, its active object is not destroyed, because the scope chain of the anonymous function is still referencing the active object. In other words, when the createcomparisonfunction () function returns, the scope chain of its execution environment is destroyed, but its active objects remain in memory until the anonymous function is destroyed, createcomparisonfunction () Object will be destroyed.
// create function comparenames = createcomparisonfunction ( name " // call function var result = Comparenames ({name: nicholas }, {name: " greg // dereference a reference to an anonymous function (to free memory) comparenames = null ;
The comparison function created is saved in the variable comparenames. By setting comparenames equal to NULL to dismiss the function's reference, it is tantamount to notifying the garbage collection routine to purge it. As the scope chain of anonymous functions is destroyed, other scopes (in addition to the global scope) can be safely destroyed.
Closures and variables
function Createfunctions () { varnew Array (); for (var i=0; i++) { = function () { return i; }; } return result;}
Each function returns 10. Because the active objects of the createfunctions () function are kept in the scope chain of each function, they refer to the same variable i.
function Createfunctions () { varnew Array (); for (var i=0; i++) { = function (num) { return function () { return num; }; } (i); } return result;}
Closures and This,arguments
Each function will automatically get 2 special variables when called: this and arguments. When searching for these 2 variables, the intrinsic function only searches for its active object, and cannot directly access the 2 variables in the external function. You can access the this object in the external scope by saving it in a variable that the closure can access. The same is true of arguments.
varName ="The Window";var Object={name:"My Object", Getnamefunc:function () {varthat = This; returnfunction () {returnThat.name; }; }};alert (Object. Getnamefunc () ());//"My Object"
Mimic block-level scopes
function Outputnumbers (count) { for (var i=0; i < count; i++) { alert (i); } // Count }
Commonly referred to as private scopes
(function() { // This is a block-level scope });
function Outputnumbers (count) { (function () { for (var i=0; i < count; i++) { alert (i); } }) (); // cause an Error! }
Private variables
function MyObject () { // private variables and private functions var privatevariable = 10; function Privatefunction () { Span style= "color: #0000ff;" >return false ; // Privileged method this . Publicmethod = function () {privat evariable ++; return Privatefunction (); };}
function person (name) { this . getName = function () { Span style= "color: #0000ff;" >return name;}; this . SetName = function = value; };} var person = new person ("Nicholas" // "Nicholas" person.setname (" Greg ""; alert (Person.getname ()); // "Greg"
Static private variables
(function(){ //private variables and private functions varPrivatevariable = 10; functionprivatefunction () {return false; } //constructor Function MyObject = function () {}; //Public/Privileged MethodsMyObject.prototype.publicMethod =function() {privatevariable++; returnprivatefunction (); };}) ();
Undeclared variable, creates a global variable
But when you use the prototype problem, the instance variable needs to be defined by itself
(function(){ varName = ""; person=function(value) {Name=value; }; Person.prototype.getName=function(){ returnname; }; Person.prototype.setName=function(value) {Name=value; };}) ();varPerson1 =NewPerson ("Nicholas"); alert (Person1.getname ()); //"Nicholas"Person1.setname ("Greg"); alert (Person1.getname ()); //"Greg"varPerson2 =NewPerson ("Michael"); alert (Person1.getname ()); //"Michael"Alert (Person2.getname ());//"Michael"
Module mode
Single-instance objects
var singleton = { name:value, function () { // Here is the code for the method }};
The module mode is enhanced by adding private variables and privileged methods to the singleton
varSingleton =function(){ //private variables and private functions varPrivatevariable = 10; functionprivatefunction () {return false; } //Privileged/Public methods and properties return{publicproperty:true, Publicmethod:function() {privatevariable++; returnprivatefunction (); } };} ();
Each singleton created in this mode is an instance of object, because it is ultimately represented by an object literal. A singleton is usually present as a global object, does not pass it to a function, and does not use instanceof to check its object type
Enhanced Module mode
Add the code that is enhanced to the object before returning it. The appropriate scenario is that a singleton must be an instance of some type, and you must add some properties or methods to enhance it.
varSingleton =function(){ //private variables and private functions varPrivatevariable = 10; functionprivatefunction () {return false; } //Creating Objects varObject =NewCustomtype (); //Add privilege/public properties and MethodsObject.publicproperty =true; Object.publicmethod=function() {privatevariable++; returnprivatefunction (); }; //return This object returnobject;} ();
Reference:
The Chinese version of JavaScript advanced programming
Revisit JavaScript (quad)-functions