Define function: a. Function declaration function functionName (name, age) {}————Function declaration promotion
b. Function expression var functionName=function (name, age) {};
Recursion
Closure
The scope of variables is nothing more than two types: global variables and local variables.
The special thing about Javascript language is that global variables can be read directly inside the function.
How to read local variables from outside?
That is to define another function inside the function. By using the internal function as the return value, you can read its internal variables from the outside. A closure is a function that can read the internal variables of other functions.
Since in the Javascript language, only sub-functions inside the function can read local variables, closures can be simply understood as "functions defined inside a function".
So, in essence, closure is a bridge connecting the inside of a function with the outside of the function.
It has two biggest uses. One is to read the variables inside the function as mentioned earlier, and the other is to keep the values of these variables in memory.
Notes on using closures
1) Since closures will cause the variables in the function to be stored in memory, which consumes a lot of memory, closures cannot be abused, otherwise it will cause performance problems on the web page. May cause memory leak in IE. The solution is to delete all unused local variables before exiting the function.
2) Closure will change the value of the variable inside the parent function outside the parent function. Therefore, if you use the parent function as an object, the closure as its public method, and the internal variables as its private value, you must be careful not to Feel free to change the value of the variable inside the parent function.
Closure is a relatively abstract concept, especially for js novices. The explanation in the book is really obscure, and it is the same for me.
But it is also something that cannot be bypassed in improving js capabilities. The first step is a question that must be asked in almost every interview, because when you answer it, the depth of your answer, your understanding of terminology, and the description of the operation of the internal interpreter of js can all reveal your actual js level. Even if you don’t answer correctly, you can let the examiner evaluate your level. So let me first talk about my understanding of closures in js.
Closures are a feature that many languages have. In js, closures Packages mainly involve several other features of js: scope chain, garbage (memory) recycling mechanism, function nesting, etc.
Before understanding closures, it is best to understand the meaning of scope chain first. To put it simply, the scope chain is an index created when the function is defined to find the value of the variable used. Its internal rule is to put the local variables of the function itself at the front and its own The variables in the parent function are placed second, and the variables in the higher-level function are placed further behind, and so on until the global object. When the value of a variable needs to be queried in the function, the js interpreter will take action Search the domain chain, starting with the local variable at the front. If the corresponding variable is not found, go to the next level of the chain. Once the variable is found, it will not continue. If you find the variable you need, you will not find it at the end. variable, the interpreter returns undefined.
After understanding the scope chain, let’s take a look at the memory recycling mechanism of js. Generally speaking, when a function starts executing, the variables defined in it will be divided into memory spaces to save them. They are used for subsequent statements. When the function completes execution and returns, these variables are considered useless. The corresponding memory space is also reclaimed. The next time this function is executed, all variables return to their original values. Status, reassigned. But if there is another function nested inside this function, and this function may be called externally. And this internal function uses some variables of the external function. This kind of memory There will be problems with the recycling mechanism. If the internal function is called directly after the external function returns, then the internal function will not be able to read the value of the variable in the external function that it needs. Therefore, the js interpreter encounters the function definition time, the function will be automatically saved together with the variables it may use (including local variables and variables (free variables) of parent and ancestor functions). That is to say, a closure will be constructed, and these variables will not be used by the memory collector. By recycling, the closure will be destroyed only when the internal function cannot be called (for example, it is deleted, or there is no pointer), and no variables referenced by the closure will be destroyed when the next memory recycling is started. Recycled.
In other words, with closures, nested function structures can operate, which is in line with our expectations. Then, closures have some features, but they are often difficult for programmers to understand.
Look at the code below.
var result=[];function foo(){ var i= 0; for (;i<3;i=i+1){ result[i]=function(){ alert(i) } } }; foo(); result[0](); // 3 result[1](); // 3 result[2](); // 3
这段代码中,程序员希望foo函数中的变量i被内部循环的函数使用,并且能分别获得他们的索引,而实际上,只能获得该变量最后保留的值,也就是说.闭包中所记录的自由变量,只是对这个变量的一个引用,而非变量的值,当这个变量被改变了,闭包里获取到的变量值,也会被改变.
解决的方法之一,是让内部函数在循环创建的时候立即执行,并且捕捉当前的索引值,然后记录在自己的一个本地变量里.然后利用返回函数的方法,重写内部函数,让下一次调用的时候,返回本地变量的值,改进后的代码
var result=[];function foo(){ var i= 0; for (;i<3;i=i+1){ result[i]=(function(j){ return function(){ alert(j); }; })(i); } }; foo(); result[0](); // 0 result[1](); // 1 result[2](); // 2
在这里我再解释一下.这里用到了另外2个技术,立即调用的匿名函数和返回函数.也是初学者比较难以理解的部分.
私有变量
任何在函数中定义的变量,都可以是私有变量。
把有权访问私有变量和私有函数的方法称为特权方法,a)在构造函数中定义特权方法
b)在私有作用域中定义变量或函数,私有变量和函数由实例共享
模块模式——为单例创建私有变量和特权方法。单例,指只有一个实例的对象
使用一个返回对象的匿名函数,函数内部首先定义私有变量和函数,将一个对象字面量作为函数的值返回,返回的 对象字面量只包含公开的属性和方法。