標籤:資訊 type html document 工具 語句塊 fun 輸入 cti
<!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語句塊中可以省略對象名,直接操作該對象的屬性和方法,簡化代碼的輸入
javaScript with的用法