JavaScript is a scripting language that belongs to the web!
JavaScript is used by millions of of pages to improve design, validate forms, detect browsers, create cookies, and apply more.
JavaScript is the most popular scripting language on the Internet.
JavaScript is easy to use! You're going to love it!
JavaScript is an interpretive scripting language, with a flexible syntax that allows different people to spell the same function in many different ways. How do you organize JavaScript code to make it easy for people to see you at a glance? Are you looking forward to someone who reads your code and sighs, "Can you write that?"
The N ways of writing anonymous functions
The anonymous function of JS is a function that does not declare the name of the function, in the following format:
Actually on the project we are often preceded by the words ";" :
Because the syntax of JS can omit semicolons, but this mechanism will also cause unexpected errors. In order to avoid merging the code into one file after the line is down, it causes syntax errors, so add ";" You can avoid unknown errors.
But sometimes we see other people's libraries or plug-ins that write anonymous functions like this:
"+" Here is the operator, the operator has a very high priority, so the right side of the function declaration plus the part of the parentheses (in fact, is the function of the execution of the writing) directly executed. In fact, more than the front can be "+" number, "-", "!" "," ~ "," + + "and other operators are available. This is only to do the extension of the introduction, specifically what kind of writing to see the team uniform norms.
Discard Math.ceil () and Math.floor rounding
Perhaps in other code see these two symbols ~ ~ and |, see directly the results of the operation:
>> var a1 = 1.23
>> ~~a1
1
>> var a2 = 2.345
>> a2|0
2
>> var a3 =- 3.45
>> ~~a3
-3
>> var a4 = -4.5
>> a4|0
-4
Note that this type of writing is not original, but cited to analyze and explain this alternative way of writing. Simple explanation, ~ is a bitwise inverse operator that converts a floating-point number to an integer by dropping all the bits after the decimal point. Positive integers can be converted to unsigned hexadecimal values. And then take the reverse (~ ~) negative negative positive, get the original integer. is so willful not to love to adjust the method, you say calculate is also a kind of optimization.
Note: If you need to do a strict rounding operation, use this method carefully, you still have to use the math function.
If and else are not unique
Using If-else's condition to judge is very clear logic, in processing the data volume is not very simple to look at is not very concise:
if (a===1) {//Here strongly recommends strictly equal to symbol ' = = ', no type conversion
a=2
} else if (a===3) {
a=4
} else {
a=5
}
Look at the use of | | and && to thin behind the code:
((a===1) && (true,a=2) | | ((a===3) && (true,a=4) | | (a=5)
A line is done, thin body success. and && The simple principle goes without saying that the comma operator is not easy to understand and can be replaced with a ternary operator:
(a===1)? a=2: ( (a===3)? (a=4): (a=5))
This style of writing seems to be simple enough, but it's a bit hard for people to see your code.
Use ToString instead of annoying string stitching DOM structure
If you want to dynamically generate a DOM structure, this is generally how we do it:
var template = "<div>"
+ "
Summarize
All of the above JavaScript alternative writing, some for the program to understand or improve the efficiency of the syntax of sugar, such a practice is preferable, such as the previous omission of if-else practice. Some are designed to improve the compatibility and performance of our code, such as AMD and inheritance. ...... I am a rookie, the above content must have incomplete and no explanation of the place after the full complement.
The above content is for JavaScript's alternative writing the related introduction, hoped that has the help to everybody!