Using a Method as a Property and Vice Versa

Một phần của tài liệu Beginning JavaScript Third Edition phần 5 pptx (Trang 74 - 79)

The final common error is where either you forget to put parentheses after a method with no param- eters, or you use a property and do put parentheses after it.

When calling a method you must always have parentheses following its name; otherwise JavaScript thinks that it must be a property. For example, examine the following code:

var nowDate = new Date;

alert(nowDate.getMonth);

In the first line you have used the Dateconstructor, which is simply a method of the Dateobject, with no parentheses.

On the second line, you call the getMonth()method of the Dateobject, except that you’ve forgotten the parentheses here also.

This is how lines should be:

var nowDate = new Date();

alert(nowDate.getMonth());

Just as you should always have parentheses after a method, you should never have parentheses after a property, otherwise JavaScript thinks you are trying to use a method of that object:

var myString = new String(“Hello”);

alert(myString.length());

In the second line you have used parentheses after the lengthproperty, making JavaScript think it is a method. You should have written it like this:

var myString = new String(“Hello”);

alert(myString.length);

This mistake may seem like an obvious one in two lines, but it’s easy to slip up when you’re pounding out lots and lots of code.

Now that you’ve seen these top seven mistakes, you’ll take a look at one way to make remedying them easier. This is through the use of the Microsoft script debugger.

Microsoft Script Debugger

The Microsoft script debugger for use with IE is a very useful tool for discovering what’s gone wrong and why. Using it, you can halt the execution of your script and then step through code line by line to see exactly what is happening.

You can also find out which data are being held in variables and execute statements on the fly. Without the script debugger, the best you can do is use the alert()method in your code to show the state of variables at various points.

The script debugger works with IE 5+.

Obtaining the Script Debugger

You can currently download the script debugger from the following URL:

www.microsoft.com/downloads/details.aspx?FamilyID=2f465be0-94fd-4569-b3c4- dffdf19ccd99&displaylang=en

If the URL changes, a search for “script debugger” on the Microsoft web site, www.microsoft.com, ought to find its new home.

Other programs, such as Microsoft Visual Studio, also come with a built-in script debugger so there’s no need to install this one.

In addition, Windows 2000 automatically comes with the script debugger, although it may not be set up on your system. To install it, open the Control Panel and choose Add/Remove Programs. Click the Add/Remove Windows Components button, and in the window that opens, scroll down the list to the script debugger. If it is not checked, then check it and click Next to install it.

To see if the script debugger is already installed, launch Internet Explorer and select the View menu. If one of the menu options is Script Debugger, as shown in Figure 10-3, the debugger is installed. If you do not find this menu option, it is still possible that the script debugger is already installed, but disabled.

See the end of the next section to learn how to enable the script debugger, and then check for the menu option again.

Figure 10-3

Installing the Script Debugger

After downloading the script debugger, you need to install it. First, you need to run the file you have just downloaded, for example from the Windows Start bar’s Run menu option. You should then see the dialog box shown in Figure 10-4, asking whether you want to install the debugger: Click the Yes button.

Figure 10-4

Next the license screen appears, as shown in Figure 10-5. Check the license, and then click the Yes button to agree to the conditions and install the debugger.

Figure 10-5

Next you get to choose where you want to install the debugger (see Figure 10-6). Anywhere on your local machine is fine.

Figure 10-6

Click OK, and if a screen appears asking whether you want to create the directory, just click Yes.

The script debugger will now install. When it’s complete, you see the message shown in Figure 10-7.

Figure 10-7

Click OK and if asked whether you want to restart the computer, click Yes.

After the computer has restarted, open Internet Explorer. If you go to the View menu, you should see the Script Debugger option. If not, the script debugger may be disabled. To enable it in IE 5 and later, go to Tools➪Internet Options. Then select the Advanced tab to see a screen similar to that shown in Figure 10-8.

Figure 10-8

Make sure that the Disable script debugging (Internet Explorer) check box is cleared, as shown in Figure 10-8. Click OK, and then close the browser. When you reopen the browser you should see the Script Debugger option in the View menu.

Using the Script Debugger

It’s important to point out that there are actually two versions of the script debugger: the basic version that you installed in the previous section and a more sophisticated version that comes with programs like Microsoft Visual Studio .Net. The more sophisticated version does everything that the basic version does, but the screen layout and look will vary slightly, as will some of the keys and icons. You’ll be look- ing at just the basic version here, so all screenshots are applicable to that.

Opening a Page in the Debugger

You can open a page in the script debugger in a number of ways, but here you’ll just look at the three that are most useful. However, before you start let’s create a page you can debug. Note the deliberate typo in line 14. Be sure to include this typo if creating the page from scratch.

Một phần của tài liệu Beginning JavaScript Third Edition phần 5 pptx (Trang 74 - 79)

Tải bản đầy đủ (PDF)

(79 trang)