function declaration
function declaration Promotion
function expression
anonymous function/lambda function
1 recursion
2 closures (functions that have access to variables in another function scope)
2.1 Closures and variables
A closure can only get the last value that contains any variable in the function.
function createfunctions () { varnew Array (); for (var i = 0; i < i++) { function() { return i;< c16/>} } return result;
The results returned 10 of 10;
You can force a closure to behave as expected by creating another anonymous function (but it feels a bit stupid and messy)
function createfunctions () { varnew Array (); for (var i = 0; i < i++) { function(num) {return function() { return num; } }}}
2.2 About this object
The execution environment of an anonymous function is global, and this points to other objects when the function execution environment is changed by call () or apply ().
var name = "the window"; var object = { "My object", function() { return function () { returnthis. Name; } // "The Window" (in non-strict mode)
2.3 Memory leaks
If an HTML element is stored in the scope chain of the closure, it will cause the element to not be destroyed.
function Assignhandler () { var element = document.getElementById ("someelement"); var id = element.id; function () { alert (ID); }; // Important!!! NULL ;}
3 Impersonation block-level scopes
JavaScript does not have the concept of block-scoped scopes.
function Outputnumbers (count) { for (var i=0; i < count; i++) { alert (i); } // Count }
Anonymous functions can be used to mimic block-level scopes.
(function() { // This is a block-level scope });
function () { // Error!
4 Private variables
There is no concept of private members in JavaScript, and all object properties are public.
Public methods that have access to private variables and private constructors are called privileged methods.
function MyObject () { // private variable and private function var privatevariable = ten; function privatefunction () { returnfalse; }; This function () { privatevariable++ ; return privatefunction (); };}
4.1 Static Private variables
(function() { // private variable and private function var privatevariable = ten; function privatefunction () { returnfalse; } function () { }; function () { privatevariable++ ; return privatefunction (); };}) ();
4.2 Module Mode
varSingleton =function() { //private variables and private functions varPrivatevariable = 10; functionprivatefunction () {return false; } return{publicproperty:true, Publicmethod:function() {privatevariable++; returnprivatefunction (); } };};
4.3 Enhanced module mode
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;};
JavaScript function expressions