Reading
The Assignment
Introduction
This assignment is an exercise in working with objects. The assignment is to prepare a script file that creates and displays the properties of various objects.
Replace all links to scripts in your index.xhtml page with a new link to a single script file named scripts/assignment_03.js. Create that file, and edit statements into it as you work through the assignment steps.
I’m reluctant to give the following advice because, in general, it is better to pay attention to warning messages rather than to ignore them. But in preparing this assignment I got really tired of all the blue warning messages that were coming out on the Firebug console. Clicking on the dropdown menu of the Console tab and un-checking the “Show JavaScript Warnings” option really helped.
Assignment Steps
-
Create an empty object named cs903 in the global object.
First, prove that the variable named cs903 is undefined by using console.log statments to print the type of both cs903 and window.cs903. (We will explore the window object below.)
Then without using the var keyword, assign the value { } to variable named cs903.
To improve the clarity of this assignment’s output, display a copy of each assignment statement just before executing it. That is, the first four lines of your script file should be:
console.log("The type of cs903 is '" + typeof cs903 + "'."); console.log("The type of window.cs903 is '" + typeof window.cs903 + "'."); console.log("cs903 = { };"); cs903 = { };Now repeat the first two console.log() statements to display the type of cs903 and window.cs903 again.
Verify that before the assignment statement, both cs903 and window.cs903 are undefined, but after the assignment they are both objects.
Write out the answers to the following questions in the email you submit for this assignment:
- What is the type of a variable before it is assigned a value?
- Is it necessary to use the var keyword to define a variable?
- If you don’t use the var keyword to define a variable, where is that variable created?
- What is the type of the variable named window?
- Why is window called the “global object”?
-
Turn the cs903 variable into a function and execute it.
Add code to assignment_03.js to assign the value function(){ console.log("This is window.cs903"); } to the global variable cs903. Add console.log() statements to document the assignment statement and to show the new type of window.cs903.
Add statements to execute your new function:
cs903(); window.cs903();You should see the message, “This is window.cs903” displayed twice.
-
Repeat the previous step for another function, named onload()
This really is the same as the previous steps, except you can skip the part about assigning an object to the property you will use here, the window.onload property. Display the type of onload and window.onload, then assign a function to it that displays the message, “This is window.onload” (instead of “This is window.cs903”), when the function executes.
Run your code, and write out answers to the following questions:
- What was the type of the window.onload property before you assigned a value to it?
- Anything different from using cs903 yet?
- What was the type of the window.onload property after you assigned the function to it?
- Anything different from using cs903 yet?
- How many times did the “This is window.onload” message appear?
- What is different from using cs903?
- Explain the difference.
Submit
When you have tested your code and determined the answers to the questions, type your answers into the body (not an attachment) of an email message, and send it to me. I’ll copy your entire web site to my computer (babbage) and check out your code and will check your email to be sure you answered the questions correctly.
Send your email message to me at: vickeryatbabbage.cs.qc.cuny.edu by midnight of the due date.
The Subject line of your email message must be: “CS-90.3 Assignment 3.”
Don’t forget to put your name in your email message!