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面向对象的继承问题
说到面向对象这个破玩意,曾经一度我都处于很懵逼的状态,那么面向对象究竟是什么呢?其实说白了,所谓面向对象,就是基于类这个概念,来实现封装.继承和多态的一种编程思想罢了.今天我们就来说一下这其中继承的问 ...
随机推荐
- vue filters过滤器的使用
说的很详细 https://www.w3cplus.com/vue/how-to-create-filters-in-vuejs.html
- enum学习
https://www.cnblogs.com/hyl8218/p/5088287.html
- (转) shell实例手册
shell实例手册 1文件{ touch file # 创建空白文件rm -rf 目录名 # 不提示删除非空目录(-r:递归删除 -f强制)dos2uni ...
- (转)通过shell脚本实现批量添加用户和设置随机密码以及生产环境如何批量添加
通过shell脚本实现批量添加用户和设置随机密码以及生产环境如何批量添加 原文:http://www.21yunwei.com/archives/4773 有一个朋友问我如何批量创建用户和设置密码 , ...
- C和C++中include 搜索路径的一般形式以及gcc搜索头文件的路径
C和C++中include 搜索路径的一般形式 对于include 搜索的路径: C中可以通过 #include <stdio.h> 和 #include "stidio.h&q ...
- POJ 3468——A Simple Problem with Integers——————【线段树区间更新, 区间查询】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 86780 ...
- BNU4299——God Save the i-th Queen——————【皇后攻击,找到对应关系压缩空间】
God Save the i-th Queen Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %lld ...
- 【ubuntu】更换下载源
ubuntu,我们在使用apt新装软件的时候,会使用官方的网站去下载软件,但是会因为国内的转接点太多,而导致下载的速度非常慢 ,我们可以通过换成一些中间的节点来进行下载,比如阿里源,中科大源,清华源等 ...
- [转]ASP.NET Core 中文文档 第四章 MVC(4.3)过滤器
本文转自:http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_4_3-filters.html 原文:Filters 作者:Steve Smith 翻 ...
- java字节码速查笔记
java字节码速查笔记 发表于 2018-01-27 | 阅读次数: 0 | 字数统计: | 阅读时长 ≍ 执行原理 java文件到通过编译器编译成java字节码文件(也就是.class文件) ...