javascript类式继承模式#1——默认模式

本文介绍了一种简单的JavaScript类式继承模式,默认模式实现,并通过一个示例展示了如何使用该模式来扩展基类的功能。同时也指出了这种方法的一个缺点,即无法在子类构造函数中向父类传递参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>类式继承模式#1——默认模式</title>
 6 </head>
 7 
 8 <body>
 9 <script type="text/javascript">
10 
11     function Parent(name){
12         this.name=name||'Adam';
13     };
14     
15     Parent.prototype.say=function(){
16         return this.name;
17     };
18     
19     function Child(name){};
20     
21     inherit(Child,Parent);
22     
23     function inherit(C,P){
24         C.prototype=new P();        
25     }
26     
27 /***************************************/
28 
29 var kid=new Child('Janking');
30 
31 console.log(kid.say())
32 
33 //缺点:inherit()并不支持将参数传递到子构造函数中,而子构造函数然后又将参数传递到父构造函数中。
34 
35 
36 </script>
37 </body>
38 </html>

 

转载于:https://www.cnblogs.com/w3develop/p/3434865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值