首页 > 编程语言 > 详细

JavaScript--对象继承(组合继承)

时间:2017-10-29 00:15:32      阅读:317      评论:0      收藏:0      [点我收藏+]
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <script>
 7         function Person(name,sex) {
 8             this.name = name || "匿名";
 9             this.sex = sex || "男";
10         }
11 
12         // 构造函数 - 学生
13         function Student(a,b,number) {
14             // 借用继承
15             Person.call(this,a,b);
16             this.number = number;
17         }
18 
19 
20 
21         Person.prototype.sayHi = function () {
22             console.log("我是Person,我是男生");
23         }
24         // 原型继承
25         Stuent.prototype = new Person();
26         Student.prototype.constructor = Person;
27 
28         var stu1 = new Student("小明","男","一号男主角");
29         console.log(stu1);
30         stu1.sayHi();
31         console.log(stu1.name);
32     </script>
33 </head>
34 <body>
35 
36 </body>
37 </html>

 

JavaScript--对象继承(组合继承)

原文:http://www.cnblogs.com/mrszhou/p/7748659.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!