This article lists 24 suggestions that make your code writing process easier and more efficient. Maybe you are still a beginner in JavaScript and have just finished writing your own HelloWorld. There are a lot of tips that will be very useful for your work. Maybe you know some tips, so let's take a quick look, see if you can find something new! Note: This document uses the Firebug console object multiple times. For details, refer to the Firebug Console API. For more details about firebug, click here.
1. Replace = with =
There are two different equal operators in JavaScript: ===|! ==And ==|! =. In contrast, the former is more recommendable. Use the former as much as possible. Reference:
"If two comparison objects share the same type and value, = true is returned ,! = False is returned ."
-JavaScript: The Good Parts
However, if the = and! =, You may encounter unexpected problems when operating on different data types. Before equality determination, JavaScript tries to convert them into strings, numbers, or Boolean values.
2. Avoid using Eval Functions
The Eval function uses a string as a parameter and executes the string as a JavaScript statement to return the result (for reference ).
This function not only reduces the execution efficiency of your script, but also greatly increases the security risk, because it gives too much rights as text parameters. Never use it!
3. Do not use Quick writing
Technically, you can omit most of the curly arc and semi-colon. Most browsers can correctly execute the following statements:
Copy content to clipboardCode:
if(someVariableExists)
x = falseHowever, if so:
Copy content to clipboardCode:
if(someVariableExists)
x = false
anotherFunctionCall();You may think it is equal to the following statement:
Copy content to clipboardCode:
if(someVariableExists) {
x = false;
anotherFunctionCall();
}Unfortunately, this is not the case. The reality is that it is equivalent:
Copy content to clipboardCode:
if(someVariableExists) {
x = false;
}
anotherFunctionCall();As you noted, the beautiful indentation cannot replace this gorgeous curly arc. In all cases, please clearly write curly braces and semicolons. It can be omitted occasionally when there is only one line of statements, although this is not recommended below:
Copy content to clipboardCode:
if(2 + 2 === 4) return 'nicely done';
Think about the future, child.
Suppose that you need to add more commands for this if statement in the future development process? Why don't you add brackets?
4. Make good use of JS Lint
JSLint is a debugger written by Douglas Crockford. You only need to paste your code to quickly scan for any obvious errors and problems. Reference:
"JSLint scans the received code. Locate the problem, describe the problem, and give the approximate location in the source code. Possible problems include but are not limited to syntax errors, although syntax errors are indeed the most common. JSLint will also use
It is customary to check the formatting style and structure errors of the Code. Scanning through JSLint does not guarantee that your program is completely correct. It just provides you with an extra pair of eyes for error detection ."
-JSLint document
Before you complete the code, put it in JSLint and check it to quickly eliminate your unintentional experience.
5. Load the script at the bottom of the page
As shown in:
Remember-we have to do everything possible to ensure that the page loading speed on the client is as fast as possible. When the script is not loaded, the browser cannot load the rest of the page.
If your JS file only adds some additional functions-for example, bind an event to click a link-that can be done after page loading is complete. Put the JS file at the end of the page, Before the end tag of the body, it is best to do so.
A better way of writing is
Copy content to clipboardCode:
Super brother is the most handsome man in the world. Benhuoer.com is the best blog in the world.