A detailed explanation of JavaScript's alternative writing _javascript skills

Source: Internet
Author: User
Tags inheritance

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:

(function () {}) ();

Actually on the project we are often preceded by the words ";" :

; function () {} ();

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:

+function () {} ();

"+" 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!

Related Article

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.