9 traps and comments of Error Analysis in Javascript

Source: Internet
Author: User

From nine JavaScript gotchas, the following are nine traps that JavaScript is prone to mistakes. Although it is not a very profound technical problem, please note that it will make your programming easier, that is, make life easier. I will give some comments on some traps.

1. last comma
For exampleCodeNote that the last comma should be good in terms of Linguistics (this is allowed in Python's dictionary similar to data types ). Internet Explorer reports syntax errors, but it is not detailed. You can only scan thousands of lines of code.

<textarea id="runcode9437"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

2. the reference of this will change.
Such as this Code:<Input type = "button" value = "Gotcha! "Id =" mybutton ">
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

The answer is not "javascript rules", as you wish ". When executing myobject. clickhandler, this row is red in the Code. The reference of this actually points to the reference of document. getelementbyid ("thetext. It can be solved as follows:<Input type = "button" value = "Gotcha! "Id =" thetext ">
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

Essentially, this is the Javascript scope issue. If you see it, you will find that there are more than one solution.

3. Identify thieves
Do not use variable names with the same html id in JavaScript. The following code:

<Input type = "button" id = "thebutton">
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

IE will report an undefined object error. I can only say: IE sucks.

4. Replace the string with the first match
The following code:

[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

In fact, the result is "this_is a title". In JavaScript, the first parameter of string. Replace should be a regular expression. Therefore, the correct method is as follows:

VaR filename = "this is a title". Replace (// G ,"_");

5. mouseout sometimes means mousein
In fact, this is caused by event bubbling. In IE, there are mouseenter and mouseleave, but they are not standard. The author suggests that you use library, such as Yui, to solve the problem.

6. parseint is based on the hexadecimal system.
This is common sense, but many people ignore parseint and have a second parameter to indicate the hexadecimal notation. For example, parseint ("09"), if you think the answer is 9, it is wrong. Because, here, the string starts with 0 and parseint processes it in octal. In octal, 09 is invalid, false is returned, and Boolean value false is converted to a value of 0. therefore, the correct method is parseint ("09", 10 ).

7. For... in... will traverse all things
There is a piece of code like this:

VaR arr = [5, 10, 15]
VaR Total = 1;
For (var x in ARR ){
Total = total * arr [x];

Running well, isn't it? But one day it quit, and the returned value is Nan, dizzy. I just introduced a library. It turns out that this library has rewritten the prototype of array. In this way, our arr has no more attribute (method) than that of... in... it will be traversed. Therefore, this is safer:

For (VAR x = 0; x <arr. length; X ++ ){
Total = total * arr [x];

In fact, this is also an example of the damage caused by prototype of the pollution basic class.

8. Event processor traps
In fact, only the event processor used as the object property will exist. For example, the code window. onclick = myonclickmethod will re-write the previous window. onclick event and cause IE content leakage (sucks again ). Before ie supports Dom 2 event registration, we recommend that you use libraries to solve the problem, such as using Yui:

Yahoo. util. event. addlistener (window, "click", myonclickmethod );

This should also be a common sense problem, but it may be easy for new users to make mistakes.

9. Focus pocus
Create an input text element and move the focus to it. It is reasonable to say that such code should be natural:

VaR newinput = Document. createelement ("input ");
Document. Body. appendchild (newinput );
Newinput. Focus ();
Newinput. Select ();

However, ie will report an error (sucks again and again ). The reason may be that the element does not exist when you execute focus. Therefore, we can delay the execution:

VaR newinput = Document. createelement ("input ");
Newinput. ID = "thenewinput ";
Document. Body. appendchild (newinput );
SetTimeout (function () {// here I used closure to rewrite it. If you are interested, you can compare it with the original article.
Document. getelementbyid ('thenewinput'). Focus ();
Document. getelementbyid ('thenewinput'). Select () ;}, 10 );

In practice, there are many more JavaScript traps, mostly because the parser implementation is not in place. These things generally do not appear in textbooks. They can only be shared by developers. Thank God, we live in the Internet era. We can find answers to many problems in Google.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.