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面向对象的继承问题
说到面向对象这个破玩意,曾经一度我都处于很懵逼的状态,那么面向对象究竟是什么呢?其实说白了,所谓面向对象,就是基于类这个概念,来实现封装.继承和多态的一种编程思想罢了.今天我们就来说一下这其中继承的问 ...
随机推荐
- 01-oracle限定查询-20190404
关系型数据库和半结构化数据(xml文件) oracle12c:c代表云计算 PDB,CDB sql语句执行顺序: 第一步:from子句控制数据来源: 第二步:where子句使用限定符对数据行过滤: 第 ...
- 使用Redis 配置替换fastjson 反序列化报错 com.alibaba.fastjson.JSONException: autoType is not support
新建的GenericFastJson2JsonRedisSerializer里面添加白名 添加: static { ParserConfig.getGlobalInstance().ad ...
- 段错误:使用opencv打开视频流
段错误:使用opencv打开视频流时报这个错误 1 使用命令dmesg 发现是libavutil.so模块发生了错误. 如果是java端报错,可能如下: libavutil.so ... av_di ...
- Nginx设置日志分割方法
目标:nginx cronolog日志分割配置文档,每分钟分割一次NGINX访问日志. 大体步骤如下: 1.nginx日志配置 access_log /var/log/nginx/access.log ...
- Apache Beam中的函数式编程理念
不多说,直接上干货! Apache Beam中的函数式编程理念 Apache Beam的编程范式借鉴了函数式编程的概念,从工程和实现角度向命令式妥协. 编程的领域里有三大流派:函数式.命令式.逻辑式. ...
- JavaScript 闭包初认识
1.简单的例子 首先从一个经典错误谈起,页面上有若干个div, 我们想给它们绑定一个onclick方法,于是有了下面的代码 <ul id="divTest"> < ...
- spark scala 例子
object ScalaApp { def main(args: Array[String]): Unit = { var conf = new SparkConf() conf.setMaster( ...
- 设置session超时的三种方式
设置session超时的三种方式 1. 在容器中设置:如在tomcat-7\conf\web.xml中设置 Tomcat默认session超时时间为30分钟,可以根据需要修改,负数或0为不限制sess ...
- iOS instruments之ui automation的简单使用(高手绕道)
最近使用了几次instruments中的automation工具,现记录下automation的简单使用方法,希望对没接触过自动化测试又有需求的人有所帮助. UI 自动测试是iOS 中重要的附加功能 ...
- Java集合篇一:ArrayList
package com.test.collection; /** * 自定义ArrayList容器 * * 1.实现原理:底层封装数组 * * 2.查询 * LinkList 相较 ArrayList ...