【JavaScript创建对象】

JavaScript 中的所有事物都是对象:字符串、数字、数组、日期,等等。

创建对象的四种方式:

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title></title>
 6     </head>
 7     <body>
 8         <script type="text/javascript">
 9         //通过字面量
10         var obj={};
11         //alert(typeof obj);  
12         //通过字面量
13         var plane={
14             username:"长江一号",
15             jihao:210,
16             color:'pink',
17             '100px',
18             height:'200px'
19         }
20         document.write(plane.username+plane.jihao+plane.color+plane.width+plane.height+"<hr />");
21         //通过new object()创建
22         var plane1=new Object();
23             plane1.username="长江一号";
24             plane1.jihao=210;
25             plane1.color='pink';
26             plane1.width='100px';
27             plane1.height='200px';
28             document.write(plane1.username+plane1.jihao+plane.color+plane.width+plane.height+"<hr />");
29         //通过构造函数创建
30         function plane2(){
31             this.username="长江一号";
32             this.jihao=210;
33             this.color='pink';
34             this.width='100px';
35             this.height='200px';
36             //return username;
37         }        
38         //alert(plane2());
39         var obj2=new plane2();
40         document.write(obj2.username+obj2.jihao+obj2.color+obj2.width+obj2.height+"<hr />")
41         //通过Object.create()
42         var plane3=Object.create({
43             username:"长江一号",
44             jihao:210,
45             color:'pink',
46             '100px',
47             height:'200px'
48         })
49         document.write(plane3.username+plane3.jihao+plane3.color+plane3.width+plane3.height)
50         </script>
51     </body>
52 </html>
原文地址:https://www.cnblogs.com/zxnn/p/8244215.html