Do a little bit of performance improvement for JavaScript!
This article lists 24 suggestions to make your code writing process easier and more efficient. Maybe you're a JavaScript beginner, just finishing up your Hello world, and there's a lot of tips that will be useful to your work; maybe some of the tips you already know, try a quick glance and see if you can find something new!
Note: The Firebug console object is used many times in this article, please refer to the Firebug console API. For a more detailed description of Firebug, please bash here.
1. There are two different equality operators in place of = = = In lieu of ==javascript: ===|! = = and ==|! = By contrast, the former is more worthy of recommendation. Please try to use the former.
"If two comparison objects have the same type and value, = = = Return true,!== returns FALSE." ”
–javascript:the Good Parts
However, if you use = = and!=, you may encounter some unexpected problems when manipulating different data types. JavaScript attempts to convert them to a string, number, or Boolean before making equality judgments.
2. Avoid using the Eval function
The Eval function takes a string as a parameter and executes the string as a JavaScript statement, returning the result (reference).
Not only does this function reduce the execution efficiency of your script, but it also greatly increases the security risk because it gives too much power to the arguments as text. Don't use it!
3. Do not use the quick wording
Technically, you can omit most of the curly braces and the end of the semicolon, and most browsers will correctly execute the following statement:
.if(someVariableExists)
x = false
However, if this is the case:
.if(someVariableExists)
x = false
anotherFunctionCall();
You might think it's the same as the following statement:
if(someVariableExists) {
x = false;
anotherFunctionCall();
}
Unfortunately, that is not the case. The reality is that it is equivalent to:
As you can see, the beautiful indentation is no substitute for this gorgeous curly bracket. In all cases, write clearly the curly braces and the end of the sentence semicolon. It can be omitted occasionally when there is only one line of statements, although it is highly deprecated to do so:
if(2 + 2 === 4) return 'nicely done';
Think more about the future, son.
Suppose you need to add more commands to this if statement in the future development process? And you're not going to have to add the parentheses?