15-自定义对象(构造函数)

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8 <script>
 9 
10     //需求:多个自定义对象。
11     //缺点:代码冗余,方式比较low。当我们创建空白对象的时候:new Object();
12     //利用构造函数自定义对象。
13 
14     var stu1 = new Student("王五");
15     var stu2 = new Student("赵六");
16 
17     console.log(stu1);
18     stu1.sayHi();
19 
20     console.log(stu2);
21     stu2.sayHi();
22 
23 //    console.log(typeof stu1);
24 //    console.log(typeof stu2);
25 
26     //创建一个构造函数
27     function Student(name){
28         //构造函数中的对象指的是this。
29         this.name = name;
30         this.sayHi = function () {
31             console.log(this.name+"说:大家好!");
32         }
33     }
34 
35 
36 </script>
37 </body>
38 </html>
原文地址:https://www.cnblogs.com/BingBing-Deng/p/10267143.html