As we all know, JavaScript is a very popular programming language. Developers can use it not only to develop dazzling web programs, but also to develop some mobile applications (such as PhoneGap or Appcelerator). It also has some services Side implementations, such as NodeJS, Wakanda and other implementations. In addition, many developers choose JavaScript as their entry-level language and use it to make small things such as pop-up windows.
In this article, the author will share with you JavaScript development tips, best practices and other very practical content. Whether you are a front-end developer or a server-side developer, you should take a look at these tips. , they will definitely benefit you.
The code snippets provided in this article have been tested with the latest version of Chrome 30, which uses the V8 JavaScript engine (V8 3.20.17.15).
1. When assigning a value to a variable for the first time, don’t forget the var keyword
Assign a value to an undeclared variable, and the variable will be automatically created as a global variable. In JS development, you should avoid using global variables.
2. Use === to replace ==
And never use = or! =.
3. Use semicolon as line terminator
It is a good practice to use semicolons where lines terminate. Even if the developer forgets to add a semicolon, the compiler will not have any clue because in most cases, the JavaScript parser will add it automatically.
4. Create constructor
5. Typeof, instanceof and constructor should be used carefully
6. Create a Self-calling function
This is often referred to as a self-calling anonymous function or an immediately called function expression (LLFE). When the function is created, it will be executed automatically, such as the following:
7. Create a random item for the array
8. Obtain a random number in a specific range
The following code is very versatile when you need to generate fake data for testing, such as getting a random value before the minimum salary and the maximum.
9. Generate a set of random numbers between the number 0 and the maximum number
10. Generate a random set of alphanumeric characters
11. Scramble the digital array
12. String tim function
The trim function can remove whitespace characters from a string and can be used in Java, C#, PHP and other languages.
13. Array append
14. Convert parameter object to array
15. Verify whether a given parameter is a number
16. Verify that a given parameter is an array
Note that if the toString() method is overridden, you will not get the expected results.
Or you can write like this:
Similarly, if you use multiple frames, you can use instancesof. If there is too much content, the result will also be wrong.
17. Get the maximum and minimum values from the numeric array
18. Clear the array
19. Do not use delete to delete items from an array
Developers can use split instead of delete to delete array items. Instead of deleting undefined items in an array, use delete instead.
The delete method should delete an object property.
20. Use the length attribute to shorten the array
As mentioned above to clear the array, developers can also use the length attribute to shorten the array.
If the array length value you define is too high, the length of the array will change and some undefined values will be filled into the array. The length property of the array is not read-only.