Learning JavaScript tutorial – JavaScript with Statement

Learning JavaScript tutorial – JavaScript with Statement

JavaScript offer lots of verity whiling dealing with the statements. Along with the flow control and loop control statements JavaScript supports object-related "with statements". With statement allows using a shorthand notation when referring objects and deals with the object scope chain.

with (object)
{
  statement(s);
}

Let us consider an example of the document object allows us to access the objects that compose a document. Using JavaScript with Statement we can access the scope of document object.

with (document)
{
  write("I Love JavaScript");
}

While working with JavaScript "with statement" blocks take care not to made any reference to other methods and properties. References to other methods and properties from JavaScript "with statement" block results in JavaScript Error.

Now consider the navigator object which provides information about the browser in use. Using JavaScript with Statement we can access all the Properties and methods of the navigator object.

<script type="text/javascript">
var navProperty="";
with (navigator)
{
 navProperty = navProperty + " PlatForm : " + platform;
 navProperty = navProperty + "\n Code Name : " + appCodeName;
 navProperty = navProperty + "\n Language : " + userLanguage;
 navProperty = navProperty + "\n Java Enable : " + javaEnabled();
}
alert(navProperty);
</script>

 

Leave a Reply

Your email address will not be published. Required fields are marked *