0

0

JS中的继承方式有哪些?

零下一度

零下一度

发布时间:2017-06-28 09:57:39

|

1056人浏览过

|

来源于php中文网

原创

1. 原型链继承

Child.prototype = new Parent();

 1 function Parent (name, age) { 2     this.name = name; 3     this.age = age; 4 } 5 Parent.prototype.say = function(){ 6     console.log('hello, my name is ' + this.name); 7 }; 8 function Child() { 9 }10 Child.prototype = new Parent('pursue');11 var child1 = new Child();12 child1.say(); //hello, my name is pursue13 var child2 = new Child();14 console.log(child1.say === child2.say);//true15 console.log(child1.name === child2.name);//true

 

方正魏碑简体
方正魏碑简体

魏碑是指南北朝时期北朝的碑刻书法作品。现存的魏碑书体都是楷书,因此有时也把这些楷书碑刻作品称为“魏楷”。魏碑原本也称北碑,在北朝相继的各个王朝中以北魏的立国时间最长,后来就用“魏碑”来指称包括东魏、西魏、北齐和北周在内的整个北朝的碑刻书法作品。这些碑刻作品主要是以“石碑”、“墓志铭”、“ 摩崖”和“造像记”的形式存在的。 构扁方疏朗,内紧外松,多出隶意。此墓志虽属正书,行笔却不拘一格,风骨内敛,自

下载

2. call(thisObj, param1, param2,...)

 1 function Parent(username){ 2     this.username = username; 3     this.hello = function(){ 4       alert(this.username); 5     } 6   } 7   function Child(username,password){ 8     Parent.call(this,username); 9     this.password = password;10     this.world = function(){11       alert(this.password);12     }13   }14   var parent = new Parent("zhangsan");15   var child = new Child("lisi","123456");16   parent.hello();17   child.hello();18   child.world();

3. apply(thisObj, [param1,param2,...])

 1 function Parent(username){ 2     this.username = username; 3     this.hello = function(){ 4       alert(this.username); 5     } 6   } 7   function Child(username,password){ 8     Parent.apply(this,new Array(username)); 9     this.password = password;10     this.world = function(){11       alert(this.password);12     }13   }14   var parent = new Parent("zhangsan");15   var child = new Child("lisi","123456");16   parent.hello();17   child.hello();18   child.world();

 

4. 组合继承(call+原型链 / apply+原型链)

 1 function Parent(hello){ 2     this.hello = hello; 3   } 4   Parent.prototype.sayHello = function(){ 5     alert(this.hello); 6   } 7   function Child(hello,world){ 8     Parent.call(this,hello);//利用 call 方法 将父类的属性继承过来 9     //Parent.apply(this,new Array(hello));//利用 apply 方法 将父类的属性继承过来10     this.world = world;//新增一些属性11   }12   Child.prototype = new Parent();//将父类的方法继承过来13   Child.prototype.sayWorld = function(){//新增一些方法14     alert(this.world);15   }16   var c = new Child("zhangsan","lisi");17   c.sayHello();18   c.sayWorld();

 

5.寄生组合继承,与4相似,只是将原型链换做了Object.create(Parent.prototype)

 1 function Parent(hello){ 2     this.hello = hello; 3   } 4   Parent.prototype.sayHello = function(){ 5     alert(this.hello); 6   } 7   function Child(hello,world){ 8     Parent.call(this,hello);//利用 call 方法 将父类的属性继承过来 9     //Parent.apply(this,new Array(hello));//利用 apply 方法 将父类的属性继承过来10     this.world = world;//新增一些属性11   }12   Child.prototype = Object.create(Parent.prototype);//将父类的方法继承过来13   Child.prototype.sayWorld = function(){//新增一些方法14     alert(this.world);15   }16   var c = new Child("zhangsan","lisi");17   c.sayHello();18   c.sayWorld();

 

相关专题

更多
Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

11

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

4

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

16

2026.01.21

Python多线程合集
Python多线程合集

本专题整合了Python多线程相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.21

java多线程相关教程合集
java多线程相关教程合集

本专题整合了java多线程相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.21

windows激活码分享 windows一键激活教程指南
windows激活码分享 windows一键激活教程指南

Windows 10/11一键激活可以通过PowerShell脚本或KMS工具实现永久或长期激活。最推荐的简便方法是打开PowerShell(管理员),运行 irm https://get.activated.win | iex 脚本,按提示选择数字激活(选项1)。其他方法包括使用HEU KMS Activator工具进行智能激活。

2

2026.01.21

excel表格操作技巧大全 表格制作excel教程
excel表格操作技巧大全 表格制作excel教程

Excel表格操作的核心技巧在于 熟练使用快捷键、数据处理函数及视图工具,如Ctrl+C/V(复制粘贴)、Alt+=(自动求和)、条件格式、数据验证及数据透视表。掌握这些可大幅提升数据分析与办公效率,实现快速录入、查找、筛选和汇总。

6

2026.01.21

毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm
毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm

毒蘑菇VOLUMESHADER_BM测试网站网址为https://toolwa.com/vsbm/,该平台基于WebGL技术通过渲染高复杂度三维分形图形评估设备图形处理能力,用户可通过拖动彩色物体观察画面流畅度判断GPU与CPU协同性能;测试兼容多种设备,但中低端手机易卡顿或崩溃,高端机型可能因发热降频影响表现,桌面端需启用独立显卡并使用支持WebGL的主流浏览器以确保准确结果

25

2026.01.21

github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

7

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
React 教程
React 教程

共58课时 | 3.9万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 2.3万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号