JavaScript 中的所有事物都是对象:字符串、数字、数组、日期,等等。
创建对象的四种方式:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript">
//通过字面量
var obj={};
//alert(typeof obj);
//通过字面量
var plane={
username:"长江一号",
jihao:210,
color:'pink',
width:'100px',
height:'200px'
}
document.write(plane.username+plane.jihao+plane.color+plane.width+plane.height+"<hr />");
//通过new object()创建
var plane1=new Object();
plane1.username="长江一号";
plane1.jihao=210;
plane1.color='pink';
plane1.width='100px';
plane1.height='200px';
document.write(plane1.username+plane1.jihao+plane.color+plane.width+plane.height+"<hr />");
//通过构造函数创建
function plane2(){
this.username="长江一号";
this.jihao=210;
this.color='pink';
this.width='100px';
this.height='200px';
//return username;
}
//alert(plane2());
var obj2=new plane2();
document.write(obj2.username+obj2.jihao+obj2.color+obj2.width+obj2.height+"<hr />")
//通过Object.create()
var plane3=Object.create({
username:"长江一号",
jihao:210,
color:'pink',
width:'100px',
height:'200px'
})
document.write(plane3.username+plane3.jihao+plane3.color+plane3.width+plane3.height)
</script>
</body>
</html>