Javascript has several error prone records

  • 2020-03-30 04:25:55
  • OfStack

Clear the input tag when it's worth doing, instead of using HTML (""), use val("")

2. If the event is bound multiple times, the callback function will be executed multiple times after the event is triggered, so it is better to write the function that binds the event separately, or to unbind it every time when the binding must be repeated

If the return is an array of jquery objects, use each to do the operation. Otherwise, if you operate directly on the array, you may only operate on the first object of the array without traversal

4. Case sensitivity, such as if instead of if. ToString () is toString(), and toUpperCase is toUpperCase

5. Document. GetElementById (" "), the parameters in parentheses remember quotes

SetTimeout (" function ", delay time); Function in quotes

7. Array traversal is best done through a for loop, not through for in, because for... In is much less efficient than for. There is also a performance overhead when calling the array length property, so it is best to first assign the array length property to a variable to improve performance.

8. About functions, this is the most important object in javascript, and we can pass it as an argument. The easiest thing to do here is setTimeout, the argument to this function call, which is just a function.

9. Sequence of execution:
The program executes, top to bottom, and if you hit alert, the browser stops there and doesn't continue executing until you click ok. If the script is inside the head and alert is global, it displays first, and then loads the body. If alert is inside the function, it will only work if you call the function.


Related articles: