1) closure
To understand closures, we need to understand the JS garbage collection mechanism, which means that when a function is executed, its scope is reclaimed. If a closure is formed, its scope is not reclaimed after execution.
Closures are functions that can read variables inside other functions
A function defined inside a function
A closure is a bridge that connects the inside and outside of a function
If a function is referenced by a variable other than its parent function, it forms a closure. A common way to create closures is to create another function inside one function
The purpose of a closure is to store a variable that is private to the outside world through an interface (method) that is not directly accessible to the outside world. Private variables can use closures
Example (using closures) : Closures are prone to unintended memory leaks
var test=(function(){ var a=0; return function(){ ++a console.info(a); } })() test(); // 1 test(); // 2 test(); / / 3Copy the code
Example (without closure)
var test=function() { var a=0; ++a; console.info(a); } test(); // 1 test(); // 1 test(); / / 1Copy the code
The above example forms a closure. After the first execution of the function, the scope is not destroyed by the recycle mechanism, so variable A is also saved. Each time test is called, a increments by one. If no closure is formed, each time test is called, a will be destroyed and re-created, and the result will be 1
2) Scope
In simple terms, a scope is a variable. For example, we create a function a1 that contains a subfunction A2. There are three scopes:
Global scope -A1 scope -A2 scope; That is, the global scope contains the scope of A1, and the scope of A2 contains the scope of A1.
When A1 searches for variables, it will first search from its scope and then search for the upper level of scope of A2 if it cannot find the variables. If it has not found the variables, it will search for the global scope, thus forming a scope chain.
3) prototype
Everything is an object in javascript, and every object has a _proto_ property (created automatically by the browser) that points to its prototype. When an Object is looking for an attribute, it will look for its prototype based on _proto_. If there is none, it will look for object.prototype. _proto_ to be null, thus forming the prototype chain.
What’s Prototype? ! We know that you can create a class using a constructor. Prototype makes the properties of the class inherit. So only constructors have the prototype property.
The following is directly verified by code:
function Foo(){ this.name='xiaoming' } var foo=new Foo(); foo.age="22" console.info(foo.name) //xiaoming ; Foo integrates foo's name property console.info(foo.__proto__) //object(); //true console.info(foo.__proto__ === foo.prototype) //undefine; Only constructors have the prototype property console.info(foo.constructor); / / function Foo () {... }; Actually foo and no constructor properties, he just inherited consturctor attributes of prototype console. Info (foo constructor = = = foo prototype. Constructor) / / trueCopy the code
What happens in the new operation?
• Create an empty object based on the constructor’s Prototype property and assign its reference to this, inheriting the function’s prototype
• Use this to add properties and methods to the object
• Finally return the new object to which this refers, i.e., the instance (if no other object is returned manually, return the object manually)