<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> <script type="text/javascript"> function Super(name) { this.name=name; this.color="red"; } Super.prototype.sayName=function () { alert(this.name); } function sub(name,age) { Super.call(this,name) this.age=age; } sub.prototype=new Super(); sub.prototype.constructor=sub; sub.prototype.sayAge=function () { alert(this.age) } var supTest=new Super("chengkai")//父函数的属性不受影响。 var subTest=new sub("kai",23);//子函数也可以调用父函数的函数,同时传递自己定义的属性。 //sub.prototype.sayAge(); //sub.prototype.sayName(); subTest.sayAge(); subTest.sayName(); supTest.sayName(); alert(supTest.color); </script> </html>
寄生组合式继承
最新推荐文章于 2024-05-01 21:47:18 发布