JavaScript--對象繼承(組合繼承)
阿新 • • 發佈:2017-10-29
color utf-8 doc ctype meta cti cnblogs log 組合
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--對象繼承(組合繼承)