In daily work, javascript some common shorthand skills, will directly affect our development efficiency, now the common skills are organized as follows:
1. Null (null, undefined) validation
When we create a new variable, we usually verify that the value of the variable is empty (null) or undefined (undefined). This is an often-considered validation for JavaScript programming.
Non-optimized code:
if NULL ") {Let variable2 = variable1;}
Optimized code:
In the Google Explorer developer panel under the console:
2. Arrays
Non-optimized code:
New Array (); a[0"myString1"; a[1"myString2"; a[2"myString3";
Optimized code:
Let a = ["MyString1", "MyString2", "MyString3"];
3. If true: Else optimization
Non-optimized code:
Let big; if Ten true;} Else false;}
Optimized code:
Let big = x > 10? True:false;
In the Google Explorer developer panel under the console:
4. Variable declaration
Non-optimized code:
3;
Optimized code:
Let x, Y, z=3;
5. Simplification of assignment statements
Non-optimized code:
x=x+11; y=y*;
Optimized code:
X++;minuscount--;y*=10;
Assuming x=10,y=5, then the basic arithmetic operation can be done using the following shorthand method:
In the Google Explorer developer panel under the console:
6. Avoid using RegExp objects
Non-optimized code:
var New REGEXP ("\d+ (.) +\d+","igm"= re.exec ("padding 01234 text text 56789 padding"//" 01234 text text 56789 "
Optimized code:
var result =/d+ (.) +d+/igm.exec ("padding 01234 text text 56789 padding"); Console.log (result); "01234 text text 56789"
7. If condition Optimization
Non-optimized code:
if true)
Optimized code:
if (likejavascript)
8. Alternatives to CharAt ()
Non-optimized code:
" myString ". CharAt (0);
Optimized code:
"MyString" [0]; Return ' m '
Summary of JavaScript shorthand tips