js面向对象 继承
<script type="text/javascript">
//类的声明
function Animal() {
this.name = 'name'
}
// es6中的class的声明
class Animal2 {
constructor() {
this.name = name
}
}
</script>
console.log(new Animal(), new Animal2());
/**
* 借助构造函数实现继承
*/
function Parent1() {
this.name = 'parent1'
}
function Child1() {
Parent1.call(this);
this.type = 'child1';
}
console.log(new Child1());
function Parent1() {
this.name = 'parent1'
}
Parent1.prototype.say = function(){}; function Child1() {
Parent1.call(this);
this.type = 'child1';
}
console.log(new Child1());
/**
* 借助原型链实现即成
*/
function Parent2() {
this.name = "parent2";
}
function Child2() {
this.type = "child2"
}
Child2.prototype = new Parent2();
console.log(new Child2());
/**
* 借助原型链实现即成
*/
function Parent2() {
this.name = "parent2";
this.play = [1,2,3]
}
function Child2() {
this.type = "child2"
}
Child2.prototype = new Parent2(); var s1 = new Child2();
var s2 = new Child2();
s1.play.push(4); console.log(s1.play,s2.play);
/**
* 组合方式(结合构造函数 和 原型链的优点)
*/
function Parent3 () {
this.name = 'parent3';
}
function Child3 () {
Parent3.call(this);
this.type = 'child3'
}
Child3.prototype = new Parent3();
我们再加上方法,看看是否能避免上面的问题呢
/**
* 组合方式(结合构造函数 和 原型链的优点)
*/
function Parent3 () {
this.name = 'parent3';
this.play = [1,2,3];
}
function Child3 () {
Parent3.call(this);
this.type = 'child3'
}
Child3.prototype = new Parent3();
var s3 = new Child3();
var s4 = new Child3();
s3.play.push(4);
console.log(s3.play,s4.play);
这个时候发现就不一样了,避免了eg2的缺点,这种组合方式结合了优点,弥补了缺点,这是通常实现继承的方式
4)
/**
* 组合继承的优化
*/
function Parent4 () {
this.name = 'parent4';
this.play = [1,2,3];
}
function Child4 () {
Parent4.call(this);
this.type = 'child4'
}
Child4.prototype = Parent4.prototype;
var s5 = new Child4();
var s6 = new Child4();
这样父类的原型链只执行了一次,但是还剩下一个问题,s5,s6都是父类的实例,没有自己的实例,prototype里面有个contron指明是哪个的实例,而子类的prototype拿的直接是父类的prototype,所以当然拿的是父类的构造函数
/**
* 组合继承的优化2
*/
function Parent5 () {
this.name = 'parent5';
this.play = [1,2,3];
}
function Child5 () {
Parent5.call(this);
this.type = 'child5'
}
Child5.prototype = Object.create(Parent5.prototype);
Child5.prototype.constructor = Child5;
Object.create方法创建原型对象,Parent5.prototype就是create方法的一个参数,一层一层往上找实现了继承,同时完成了继承,这个就是实现继承的完美方式
js面向对象 继承的更多相关文章
- js面向对象继承
前言 最近看到js面向对象这章节了,主要学习了原型和面向对象继承关系,为了梳理自己的知识逻辑,特此记录. js的面向对象 先说说我目前了解的js创建对象方法 1.写一个函数,然后通过new创建对象 2 ...
- 关于JS面向对象继承问题
1.原型继承(是JS中很常用的一种继承方式) 子类children想要继承父类father中的所有的属性和方法(私有+公有),只需要让children.prototype=new father;即可. ...
- js 面向对象 继承
继承方式有四种: 1.call 2.apply 3.prototype 4.for in call 和 apply 的主要区别: call 传参数只能一个一个的传, apply 因为是用数组,所以可以 ...
- JS 面向对象 ~ 继承的7种方式
前言: 继承 是 OO 语言中的一个最为人津津乐道的概念.许多 OO 语言都支持两种继承方式:接口继承 和 实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在 ...
- js 面向对象 继承机制
根据w3cschool上的描述:共有3种继承方法(对象冒充,原型链,混合) 1.对象冒充:构造函数ClassA使用this关键字给所有属性和方法赋值,使ClassA构造函数成为ClassB的方法,调用 ...
- JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- JS面向对象(2) -- this的使用,对象之间的赋值,for...in语句,delete使用,成员方法,json对象的使用,prototype的使用,原型继承与原型链
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- js面向对象之继承那点事儿根本就不是事
继承 说道这个继承,了解object-oriented的朋友都知道,大多oo语言都有两种,一种是接口继承(只继承方法签名):一种是实现继承(继承实际的方法) 奈何js中没有签名,因而只有实现继承,而且 ...
- 捋一捋js面向对象的继承问题
说到面向对象这个破玩意,曾经一度我都处于很懵逼的状态,那么面向对象究竟是什么呢?其实说白了,所谓面向对象,就是基于类这个概念,来实现封装.继承和多态的一种编程思想罢了.今天我们就来说一下这其中继承的问 ...
随机推荐
- Angular4+NodeJs+MySQL 入门-02 MySql操作类
NodeJs操作MySQL类 此类封装了几个常用的方法:插入,更新,删除,查询,开启事务,事务提交,事务回滚等操作.有一这个类,操作MYSQL就方便多了. 批处理,存储过程等方法还没有添加,因为觉得目 ...
- 01-struts2配置详解
1 struts.xml配置详解 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...
- 腾讯蔡晨:十年沉淀,腾讯iOA为企业安全保驾护航
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 背景:5月23-24日,以"焕启"为主题的腾讯"云+未来"峰会再广州召开,广东省各级政府机构领导.海 ...
- poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17805 Accepted: ...
- go test遇到的一些问题-command-line-arguments undefined: xxxxx
一 问题是在我写算法题的时候出的,test后缀的文件编译报command-line-arguments undefined: xxxxx 二 没记错,go test是 所有在以_test结尾的源码内以 ...
- Angular6路由复用与延迟加载的冲突解决——看看有备无患
结论: 结论放最上面,送给匆匆查资料的你: 同时使用延迟加载 + 路由复用,一定不能使用route.routeConfig.path做key去缓存,否则会死得难看. 经实测(我没有完全去解读源代码 ...
- Thrift笔记(四)--Thrift client源码分析
thrift文件 namespace java com.gxf.demo namespace py tutorial typedef i32 int // We can use typedef to ...
- 标准c库函数和linux系统函数的关系
c库IO函数的工作流程 c库函数与系统函数的关系 虚拟地址空间 文件描述符
- C++中 结构体和类的异同
在C++中,结构体是一种特殊形态的类. 结构体和类的唯一区别就是: 结构体和类具有不同的默认访问控制属性. 类中,对于未指定访问控制属性的成员,其访问控制属性为私有类型(private) 结构体中, ...
- JavaScript简易动画
<p id="s">fly</p> <script> function move(){ var id=document.getElementBy ...