Home  >  Article  >  Web Front-end  >  Record several error-prone points in JavaScript

Record several error-prone points in JavaScript

PHPz
PHPzOriginal
2016-05-16 16:30:141280browse

This chapter records several common mistakes in using JavaScript in projects, and always reminds yourself not to make the same mistakes again.

1. When clearing the input tag, you cannot use html("") to clear it. You must use val("") to clear it.

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

3. If the jquery selector returns a jquery For object arrays, you need to use each to operate. Otherwise, if you operate directly on the array, you may only operate on the first object of the array, and no traversal will occur.

4. Case-sensitive, for example, if is written as If . toString() is written as tostring(), toUpperCase is written as toUppercase

5.document.getElementById(""), remember to add quotation marks for the parameters in brackets

6.setTimeout("function", Delayed time); add quotation marks to the function

7. It is best to traverse the array through a for loop, not through for in, because for. . . The efficiency of in is much worse than for. At the same time, there will be a certain performance overhead when calling the length attribute of the array, so the best approach is to first assign the length attribute of the array to a variable to improve performance.

8. Regarding functions, this is the most important object in JavaScript. We can pass it as a parameter. The simplest one here is setTimeout. The parameter called by this function is actually a function.

9. The execution sequence of the program:
The program is executed from top to bottom. If an alert is encountered, the browser will stop there and will not continue execution until you click ok. If script The statement is inside the head. If the alert is global, it will be displayed first, and then the content in the body will be loaded. And if the alert is inside the function, the alert will only have an effect when you call the function.

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn