9 traps and Evaluation Analysis of JavaScript

Source: Internet
Author: User

1. last comma

For example, in this Code Section, note that the last comma should be good in terms of Linguistics (this is acceptable for a dictionary similar to the data type in python ). Internet Explorer reports syntax errors, but it is not detailed. You can only scan thousands of lines of code.

<Script>
Var theObj = {
City: "Boston ",
State: "MA ",
}
</Script>
2. the reference of this will change.

Such as this Code:

<Input type = "button" value = "Gotcha! "Id =" MyButton ">
<Script>
Var MyObject = function (){
This. alertMessage = "Javascript rules ";
This. ClickHandler = function (){
Alert (this. alertMessage );
}
}();
Document. getElementById ("theText"). onclick = MyObject. ClickHandler
</Script>
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 ">
<Script>
Var MyObject = function (){
Var self = this;
This. alertMessage = "Javascript rules ";
This. OnClick = function (){
Alert (self. value );
}
}();
Document. getElementById ("theText"). onclick = MyObject. OnClick
</Script>
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">
<Script>
TheButton = get ("TheButton ");
</Script>
IE will report an undefined object error. I can only say: IE sucks.

4. Replace the string with only the first match

The following code:

<Script>
Var fileName = "This is a title". replace ("","_");
</Script>
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 ,"_");

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.