please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.9 with

JavaScript interfaces with the web browser through an "object hierarchy" that contains quite a few arrays nested within objects and objects nested within arrays. In order to refer to the components that make up a web page, you may find yourself referring to objects with cumbersome expressions like the following:

frames[1].document.forms[0].address_field.value

The with statement provides a way to simplify expressions like this one, and reduce your typing. It has the following syntax:

with (object)
    statement

object is an expression that evaluates to an object. This specified object becomes the default object for all expressions in statement, which is a primitive statement or statement block. Any time an identifier appears within statement, that identifier is looked up as a property of object first. If the identifier is defined as a property of object, then this is the definition used. If the identifier is not defined there, then JavaScript looks up its value as it normally would.

For example, you might use the with statement to simplify the following code:

x =index.html Math.sin(i * Math.PI / 20);
y = Math.cos(i * Math.PI / 30);
Using with, you might write:

with(Math) {
    x =index.html sin(i * PI / 20);
    y = cos(i * PI / 30);
}

Similarly, instead of calling document.write() over and over again in a JavaScript program, you could use a with(document) statement, and then invoke write() over and over again instead.

You can nest with statements arbitrarily. Note that the object expression in a nested with statement may itself be interpreted depending on the object in a containing with statement.

If the object in a with statement contains properties that have the same name as top-level variables, the with statement effectively hides the top-level variable--when you use the name of that variable you now refer to the object's property instead. If you need to explicitly refer to a hidden top-level variable var, you can usually use this syntax:

top.var

We'll see why this works when we study the Window object in Chapter 11, Windows and the JavaScript Name Space. Note that this technique will not work if top is the name of a property of the object in any enclosing with statement.

It is important to understand that the with statement only works with properties that already exist in the specified object. If you assign a value to a variable that does not exist as a property of the specified object, then that property is not created in the object. Instead, JavaScript searches the containing with statements, if any, for a property with that name, and then searches for a top-level variable with that name. If no such property or variable is found, then a new top-level variable is created. The rule to remember is that new properties cannot be added to an object if you refer to the object implicitly through a with statement. To create a new property in the object, you must refer to it explicitly.

To really understand how the with statement works, we need to briefly consider how variables are looked up in JavaScript. We'll return to this topic in detail in Chapter 11, Windows and the JavaScript Name Space. Suppose JavaScript needs to look up the value of the name n. It proceeds as follows:


Previous Home Next
continue Book Index var

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch05_09.htm, 9KB, , updated: 2019/10/14 15:00, local time: 2024/11/15 20:35,
TOP NEW HELP FIND: 
3.133.137.53:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.ecomorder.com/techref/language/java/script/definitive/ch05_09.htm"> [Chapter 5] 5.9 with</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to massmind.ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .