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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值