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

复制代码
 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>
复制代码

 

posted @   琅琊丶  阅读(187)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示