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面向对象的继承问题
说到面向对象这个破玩意,曾经一度我都处于很懵逼的状态,那么面向对象究竟是什么呢?其实说白了,所谓面向对象,就是基于类这个概念,来实现封装.继承和多态的一种编程思想罢了.今天我们就来说一下这其中继承的问 ...
随机推荐
- API 接口设计工具 --Swagger
swagger-editor,无法启动GUI软件,在线版的FQ也打不开 null
- API-Framework 前后端分离
- ubuntu系统在安装好mysql后,出现ERROR 2002(HY000: Can't to local MySQL server through socket '/var/run/mysqld/mysqld.sock')(2)(图文详解)
不多说,直接上干货! 问题详情 我在写此博客之前,看了网上各种资料,写的太冗余和繁琐杂乱.最简单的解决方法莫过于我这篇博客.直接如下. 这是liux套接字网络的特性,win平台不会有这个问题. 解决方 ...
- opensuse13.2国内源和设置命令
ustc-osshttp://mirrors.ustc.edu.cn/opensuse/distribution/13.2/repo/oss/ustc-non-osshttp://mirrors.us ...
- TOJ 1883 Domino Effect
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- Shell脚本检测程序,如果挂了就重启程序
脚本如下: #!/bin/sh #要检查的进程名 PROGRESS_NAME="heihu_server" #----------------------------------- ...
- 线程中断方法interrupt() 与 cancel()
(一).关于interrupt() interrupt()并不直接中断线程,而是设定一个中断标识,然后由程序进行中断检查,确定是否中断. 1. sleep() & interr ...
- php 项目中自定义日志方法
在现在项目中之前没有定义日志的方法,每次调试起来很麻烦,经常不能输出参数,只能用写日志的方法,一直用file_put_contents很烦躁,于是用了一点时间,写了这样一个方法: <?php / ...
- fullpage的使用以及less, Sass的属性和JQuery自定义插件的声明和使用
使用fullpage的步骤 1 导入JQuery.js fullpage,js fullpage.css 2 组建网页布局,最外层id="fullpage" 单页class=& ...
- jquery解析xml
更多的项目都是在解析json,今天临时让解析几个xml文件,其实都一样,总结一下吧. 例如我们有这样一个xml文件 <?xml version="1.0" encoding= ...