Home » Online Computer Education » Learn Java Script, Manipulating Objects and With Statement, Lesson No. 27

Learn Java Script, Manipulating Objects and With Statement, Lesson No. 27

As I have already stated, JavaScript and JScript enable you to create scripts that work with objects. You can think of objects as being similar to variables except that objects can contain multiple values known as its properties. Objects also can contain built-in functions, which are collections of predefined script statements that are designed to work with the object and its data. Examples of browser objects include browser windows and form elements. Examples of WSH objects include printers and drives.

There are a number of JavaScript and JScript statements that provide you with the ability to manipulate properties associated with objects. These statements include the for…in and with statements. The for…in statement enables you to access all the properties for a specified object, and the with statement provides easy access to specific object properties and methods.

The for…in Statement
The for…in statement is used to iterate through all the properties belonging to a specified object. The syntax of the for…in statement is listed below:

for (variable in object) {
statements;
}
This statement works by creating a variable that is used to iterate through all of an object’s properties. The following example shows how to loop through all the properties belonging to the navigator object. The navigator object contains information about the type of browser being used to view the HTML page containing the JavaScript.

<HTML>
<HEAD>
<TITLE>Script 2.18 – Demonstration of the for…in statement
</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements

for (i in navigator) {
document.write(i,”<BR>”);
}

// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>

 

The with Statement
The with statement is a convenient way of saving a few keystrokes when writing your scripts. It enables you to set a default object for a group of statements. The syntax of the with statement is listed below.

with (object) {
statements;
}
The following example shows how you can use the with statement to set the document object as the default object and then apply all the statements contained within the opening and closing braces to that object. As you can see, by using this shortcut statement, I was able to type write() in place of document.write(). Although this did not save me much work in this example, it could certainly save a lot of time in situations where you need to work a lot with a given object.

<HTML>
<HEAD>
<TITLE>Script 2.19 – Demonstration of the with statement
</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/Jav
aScript”>
<!– Start hiding JavaScript statements

with (document) {
write(“The with statement saved me a few keystrokes.
<BR>”);
write(“Its use is purely discretionary!”);
}

// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published.