JS Important Knowledge points Summary-Imperfect

Source: Internet
Author: User

# # #1, closures

?? Closures are functions that can read other functions ' internal variables. Because in the JavaScript language, only sub-functions inside the function can read local variables, it is possible to simply interpret the closure as "a function defined inside a function". So, in essence, a closure is a bridge that connects the inside of the function to the outside of the function. The function of closures is to prevent global variables from flooding.

* ' note points for using closures: ' *

* 1) because the closure will make the variables in the function are stored in memory, memory consumption is very large, so can not abuse closures, otherwise it will cause the performance of the Web page, in IE may cause memory leaks. The workaround is to remove all unused local variables before exiting the function.

* 2) The closure will change the value of the inner variable of the parent function outside the parent function. So, if you use the parent function as an object, and the closure as its public method, and the internal variable as its private property (private value), be careful not to arbitrarily change the value of the inner variable of the parent function.

# # #2, variable promotion

?? Variable promotion refers to the browser parsing JS file is, the definition of variables or functions will be parsed first, and then in the parsing of other code, that is, JS can first use variables, and then in the definition. But

If the variable is initialized when it is defined, the variable will not be promoted! Attached: In the ES6 code specification, a variable must be defined before a variable can be used.

# # #3, higher order functions

?? The higher-order function is actually very simple, as long as the output is print (' Hello ') (' World '); For example:

Javascript:

var Moqi = function (p1) {

This.add = function (p2) {

return p1 + "+ p2;

};

return add;

};

Console.log (Moqi (' Hello ') (' World '));

# # #4, Callback callback function

* JQuery Callback function

When animation 100% is complete, the Callback function is called.

A typical syntax:

' $ (selector). Hide (Speed,callback); '

The callback parameter is a function that is executed after the hide operation is completed.

Error (no callback):

$ ("P"). Hide (1000); Alert ("The paragraph is now hidden");

Correct (with callback):

' $ ("P"). Hide (1000,function () {alert ("the paragraph is now hidden");}); `

Conclusion: If you want to execute a statement after a function that involves animation, use the callback function.

A function that is called by the Windows system, which is designed by you, is collectively referred to as the callback function. Some API functions require that you use callback as one of your parameters.

such as Settimer,linedda,enumobjects. The callback function is a function defined by the developer according to a certain prototype (each callback function must follow this principle to design)

JS Important Knowledge points Summary-Imperfect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.