Syntax format and usage of the with of method in javascript

  • 2020-03-30 03:39:34
  • OfStack

With statements, you don't have to specify a reference object repeatedly when accessing an object's properties and methods. The syntax of the With statement is as follows:


With Object {
Statements
}
 Object specifies a reference object when an object in a statement group defaults, and we use the familiar one here  Document  The object of  With  Examples of statements. For example,   When used with  Document  Object dependent  write( ) or  writeln( ) Methods are often used in the following form: 
document.writeln( " Hello! " )
 If you need to display large amounts of data, you will use the same multiple times  document.writeln The () statement, at this point, can be as the following program, all to  Document  Object is placed as a statement for the reference object With  Statement block, so as to achieve the goal of reducing the number of statements. Here's one With  Examples of statement usage: 
<html>
<head>
<title>JavaScript Unleashed</title>
</head>
<body>
<script type="text/javascript">
<! - 
with(document){
write(" How do you do  !");
write("<br> The title of the document is  : "" + title + "".");
write("<br> Of this document  URL  is : " + URL);
write("<br> Now you don't have to write every time  document  The prefix of the object  !");
}
// -->
</script>
</body>
</html>

This way, you can remove the document prefix when using the methods and properties of the document.


Related articles: