javaScript with的用法

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<script type="text/javascript">
//vehicle:交通工具

(function(){
function vehicle(weight,topSpeed,seats){
this.weight=weight;//车身重量
this.topSpeed=topSpeed;//最高时速
this.seats=seats;//座位数
}
var car=new vehicle("1.5t","280kph",5);
var bus=new vehicle("8t","200pkh",55);


document.write("<b>car 的信息</b>");
document.write("car的重量:"+car.weight+"<br />");
document.write("car的最高时速:"+car.topSpeed+"<br />");
document.write("car的座位数:"+car.seats+"<br />");

document.write("<br />")
document.write("<b>bus 的信息</b>");
with(bus){

document.write("bus的重量:"+weight+"<br />");     //省略Bus
document.write("bus的最高时速:"+topSpeed+"<br />");
document.write("bus的座位数:"+seats+"<br />");
}

})();
</script>
<body>
</body>
</html>

//with(对象名){

........................

}

使用with指定一个对象名,则在with语句块中可以省略对象名,直接操作该对象的属性和方法,简化代码的输入

原文地址:https://www.cnblogs.com/wxhhts/p/7783048.html