码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-10-29 00:36:36      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:style   script   asc   title   color   tor   logs   匿名   组合   

 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--对象继承(组合继承)

标签:style   script   asc   title   color   tor   logs   匿名   组合   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!