JavaScript is a commonly used scripting language in front-end development. I have sorted out some rare usage and supplemented it after being used for drying. If any of the errors is correct, please kindly advise.
1. closure, which can avoid variable contamination. Many js frameworks (such as jQuery) and plug-ins use the closure method for implementation.
(Function (args1, args2. ..) {// js code}) (args1, args2 ...);
Equivalent Syntax :! Function () {// js code} (); // This function is not recommended and may have compatibility issues.
2. Comma expression. Multiple statements can be combined into one statement.
Return alert ('ha ha! '),! 1; // comma expression, the value is the value of the last expression ,! 1 indicates false, and the data type is boolean;
3. Forcibly use the Boolean Type (! Non-zero: false ,! 0 indicates true)
Var a1 = 1, b1 = 0;
Var c1 = a1 | b1;
Alert (typeof a1 + "," + typeof b1 + "," + typeof c1); // all types are numeric
Var a2 =! 1, b2 =! 0;
Var c2 = a2 | b2;
Alert (typeof a2 + "," + typeof b2 + "," + typeof c2); // all types are Boolean
4. Use void 0 (equivalent to undefined)
Var a = void 0; // void 0 is undefined, which has strong compatibility.
Var B = undefined; // The undefined value can be overwritten. If undefined = 'abc' is added, B is equal to 'abc'