1.看例子
<script language="javascript">
function Lakers() {
this.name = "kobe bryant";
this.age = "28";
this.gender = "boy";
}
var people=new Lakers();
with(people)
{
var str = "姓名: " + name + "<br>";
str += "年齡:" + age + "<br>";
str += "性別:" + gender;
document.write(str);
}
</script>
代碼執行效果如下:
姓名: kobe bryant
年齡:28
性別:boy
2.系統對象
<script type="text/javascript">
with(location){
var hostName = hostname;
var url = href;
}
alert(hostName);
alert(url);
</script>
執行結果:
localhost
http://localhost/xxx.html