The experience of JavaScript debugging scripts 1th/2 page _javascript tips

Source: Internet
Author: User
Tags naming convention reserved
Programming is actually a technology that can evolve over time. But no matter how skilled you become, you still have to spend some time debugging your code. If you have done homework, or have jacascript programming experience, you will know that quite a lot of time is spent on debugging. This is normal-it's just one of the things programmers have to do. In fact, according to a lot of research, programmers spend an average of 50% of their time solving errors in the code.
The key is to learn how to effectively debug your program. I have some tips to help you troubleshoot why the program doesn't work as it should, or to help you avoid writing code with many errors first:
1. Print out variables in different ways
2. Attention to general errors
3. Think ahead before coding
--------------------------------------------------------------------------------
If JavaScript fails to catch your error, you also don't see the error in the code, and sometimes it helps to print out variables. The easiest way to do this is to use an alert () as follows:
Thegreeting gets a name using GetName, then presents
One or two alert boxes depending on what the name is
function GetName ()
{
var first_name = prompt ("What ' Your", "" ");
var last_name = prompt ("What ' Your last name?", "");
var the_name = first_name + "" + last_name;
Alert ("In GetName, The_name is:" + the_name);
}
-----------------------------found the error--------------------------------------------------
1. General Procedural errors
Most mistakes are just boring grammatical mistakes. Remember that closing those quotes, curly braces and parentheses takes a long time, but fortunately the JavaScript automatic error detector captures most of these errors. While JavaScript error detectors continue to evolve with increasingly complex browsers, some bugs still slip away. Here are some common errors to be aware of:
2. Confusing variable name or function name
Errors generated by uppercase and plural variables and function names are often annoying and are sometimes not captured by JavaScript error detectors. By establishing and insisting on the use of a naming convention for variables and functions, this can greatly reduce the amount of trouble. For example, I define variables all in lowercase letters and use underscores instead of spaces (My_variable,the_data, an_example_variable), with built-in symbols to represent functions (Addthreenumbers (), Writeerror (), and so on). I avoid using any complex numbers, because I always forget those variables are not plural.
3. Casual use of reserved words
Some words cannot be used as variable names because they are already in use by JavaScript. For example, you cannot define a variable called "if" because it is actually part of JavaScript-if you use "if", you will encounter all sorts of problems. When you get mad by using a variable named "If", a variable called "document" is tempting. Unfortunately, "document" is a JavaScript object. Another frequently encountered problem is to name the variable "name" (the form element has a "names" attribute). Naming a variable "name" doesn't always go wrong, but sometimes-it's even more confusing-that's why you avoid using the "name" variable.
Unfortunately, different browsers have different reserved words, so there's no way to know which words to avoid. The safest way to do this is to avoid using words and HTML that have become part of JavaScript. If you're having problems with variables and can't find out what's wrong, try changing the variable to a name. If you succeed, you may have avoided the reserved word.
4. Remember that you should use two equals in logical judgment
Some browsers can catch this error, but some cannot. This is a very common mistake, but if the browser can't point it out for you, it's hard to find. Here is an example of this error:
var the_name = prompt ("What ' Your name?", "");
if (The_name = "The Monkey")
{
Alert ("Hello monkey!");
} else {
Alert ("Hello Stranger.");
}
This code will produce "Hello monkey!" Warning dialog-no matter what you're knocking at the tip-that's not what we want. The reason is that there is only one equal sign in the If-then statement, which tells JavaScript that you want to make one thing equal to another. Let's say you're knocking "Robbie the Robot" in the tip. Initially, the value of the variable the_name is "Robbie the robot", but then the IF statement tells JavaScript that you want to set The_name to "the Monkey." So JavaScript is happy to execute your command, send a "true" message to the If-then statement, and the Result warning dialog box appears "Hello monkey!" every time. This sinister mistake will make you mad, so be careful to use two equal signs.
5. Accidentally add quotes to variables, or forget to quote strings
I come across this problem from time to times. The only way JavaScript can differentiate between variables and strings is if the strings have quotes and the variables are not. Here's an obvious error:
var the_name = ' Koko the gorilla ';
Alert ("The_name is very happy");
Current 1/2 page 12 Next read the full text
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.