Many extended features of Javascript make it more powerful, and also give programmers the opportunity to create more beautiful and user-friendly websites. Although many developers are happy to praise JavaScript, some still see its dark side. Web pages that use a lot of javascript code will load slowly, and excessive use of javascript makes web pages ugly and clunky. Soon, how to use javascript effectively became a very hot topic. Here let us list 10 best javascript practices to help you use javascript effectively. 1. Keep the code as simple as possible You may have heard this code conciseness issue many times. As a developer, you may have used it many times in your code development process, but don't forget this in js development.
2. Think before you modify your prototypes Adding new properties to an object's prototype is a common cause of script errors.
In the above code, all variables will be affected because they all inherit from yourObject. Such usage may lead to unexpected behavior. So it is recommended to delete similar modifications after use.
3. Debugging Javascript Code Even the best developers make mistakes. To minimize the chances of such mistakes, run your code in your debugger to make sure you don't run into any subtle errors. 4. Avoid Eval Your JS will work just fine without the eval method. eval allows access to the javascript compiler. If a string is passed as an argument to eval, its result can be executed. This can significantly reduce the performance of your code. Try to avoid using eval in a production environment. 5. Minimize DOM access DOM is the most complex API and will slow down the code execution process. Sometimes the web page may not load or load incompletely. ***Avoid DOM. 6. Learn JavaScript before using JavaScript libraries The internet is flooded with many javascript libraries, and many programmers tend to use js libraries without understanding the negative impact. It is strongly recommended that you learn basic JS code before using third-party libraries, otherwise, you are ready for bad luck. 7. Do not use "SetTimeOut" and "Setinterval" methods as an alternative to "Eval"
In the above code, document.getID('value') is processed as a string in the setTimeOut method. This is similar to the eval method, which executes a string in each code execution, thus reducing performance. Therefore, it is recommended to pass a method in these methods.
8. [] is better than new Array(); A common mistake is to use an object when an array is needed or an array when an object is needed. But the principle is simple:
suggestion:
avoid:
9. Try not to use var multiple times When initializing each variable, programmers are accustomed to using the var keyword. Instead, it is recommended that you use commas to avoid redundant keywords and reduce code size. As follows:
10. Don’t ignore the semicolon ";" This is often one of the reasons why people spend hours debugging. I’m sure you’ve read about this in other articles, but there are a lot of basic rules that people tend to overlook. Have you ever overlooked a semicolon? Have you ever encountered performance issues with the eval keyword? I hope you like it, thank you! |
<<: Teach you step by step how to view nearby people
>>: Google open-sources Eddystone BLE beacon communication format to challenge Apple
In the movie "Air Battle", a Mirage 200...
Since the beginning of the new year, the domestic...
When was the last time you used an air fryer? The...
Whether playing King of Glory, reading articles o...
01. Market performance Overall performance of the...
Yang Shu's "Project Management Course Ev...
Restore the user usage scenario: Register, log in...
Dynamically change application icons Product: Can...
It is also compatible with the TV version of mobi...
Recently, overseas media exposed a group of the l...
1. Overview Databinding is a framework, MVVM is a...
In Zhang Ma’s home in Guangzhou, formaldehyde lev...
Live streaming began to explode in 2015, sparking...
Around 0:22 am on December 4th, A residential bui...
Training course content: Instructor: Zhou Yiting,...