Home > Web Front-end > JS Tutorial > Syntax format and usage of with() method in javascript_javascript skills

Syntax format and usage of with() method in javascript_javascript skills

WBOY
Release: 2016-05-16 16:40:21
Original
1592 people have browsed it

Content introduction: With the With statement, there is no need to repeatedly specify the reference object when accessing object properties and methods. In the With statement block, all properties and methods that are not recognized by JavaScript are related to the object specified in the statement block. The syntax format of the With statement is as follows:

With Object {
Statements
}
对象指明了当语句组中对象缺省时的参考对象,这里我们用较为熟悉的 Document 对象对 With 语句举例。例如 当使用与 Document 对象有关的 write( )或 writeln( )方法时,往往使用如下形式:
document.writeln(”Hello!“)
如果需要显示大量数据时,就会多次使用同样的 document.writeln()语句,这时就可以像下面的程序那样,把所有以 Document 对象为参考对象的语句放到With 语句块中,从而达到减少语句量的目的。下面是一个With 语句使用的例子:
<html>
<head>
<title>JavaScript Unleashed</title>
</head>
<body>
<script type="text/javascript">
<!—
with(document){
write("您好 !");
write("<br>这个文档的标题是 : \"" + title + "\".");
write("<br>这个文档的 URL 是: " + URL);
write("<br>现在您不用每次都写出 document 对象的前缀了 !");
}
// -->
</script>
</body>
</html>
Copy after login

In this way, you can remove the Document prefix when using document's methods and properties.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template