js继承的方式
深入理解继承的实现方式不仅仅有利于自己去造轮子,封装插件,更有利于我们去阅读一些框架的源码,
以下记录几种常见的继承方式
1. 原型链实现继承
function Father(){
this.name = "爸爸"
}
Father.prototype.sayName = function(){
console.log(this.name)
}
function Son(){
this.age = 18
//this.name = "儿子" 自己有就不会沿着原型链去找
}
//子类原型对象指向父类实例实现继承
Son.prototype = new Father()
//子类添加自己的原型方法 只能写在实例后面,不然会覆盖
Son.prototype.sayAge = function(){
console.log(this.age)
}
var s1 = new Son()
s1.sayName()
s1.sayAge()
console.log(s1.name)
2. 构造函数实现继承
function Father(){
this.colors = ["red","yellow"]
this.sayColor = function(){
console.log(this.colors)
}
}
function Son(){
this.age = 18
Father.call(this)
}
var s1 = new Son()
s1.colors.push("blue")
s1.sayColor()
var s2 = new Son()
s2.sayColor()
//构造函数实现继承由于属性是在构造函数中,而不是原型中,说明这种方法没办法实现共享方法
3.组合继承
function Father(name){
this.name = name
this.colors = ["red","yellow"]
}
Father.prototype.sayName = function(){
console.log(this.name)
}
function Son(name,age){
this.age = age
Father.call(this,name)
}
Son.prototype = new Father()
Son.prototype.constructor = Son //子类原型指向子类构造函数
Son.prototype.sayAge = function(){
console.log(this.age)
}
var s1 = new Son("儿子",18)
s1.colors.push("blue")
console.log(s1.name)
console.log(s1.colors)
s1.sayName()
s1.sayAge()
//实例共享的属性方法借用原型链实现,实例私有的属性方法借用构造函数实现
4. 冒充对象实现继承
function Father(){
this.name = "爸爸"
this.sayName = function(){
console.log(this.name)
}
}
function Son(){
this.temp = Father
this.temp() //执行这个方法,内部this指向实例化的对象,实现继承
delete this.temp //内部已实现继承,销毁这个属性
}
var s1 = new Son()
s1.sayName()
console.log(s1)
5. 原型式继承
function object(o){
function F(){}
F.prototype = o
return new F()
}
var person = {
name: "张强",
sayName: function(){
console.log(this.name)
}
}
var p1= object(person)
console.log(p1.name)
p1.sayName()
//原型式继承所有属性方法全部在原型下,所有属性方法全部共享
6. 寄生式的继承
function createAnother(o){
var obj = object(o)
obj.sayHi = function(){
console.log("hi hi~")
}
return obj
}
var p2 = createAnother(person)
console.log(p2.name)
p2.sayHi()
//寄生式继承在原型式继承的基础上为新对象添加自己的属性方法
7. 寄生组合式继承
function inheritPrototype(subType,superType){
//利用中间变量实现子类的原型继承父类原型,而非父类的全部
var prototype = object(superType)
prototype.constructor = subType
subType.prototype = prototype
}
function Father(name){
this.name = name
this.colors = ["red","yellow"]
}
Father.prototype.sayName = function(){
console.log(this.name)
}
function Son(name,age){
this.age = age
Father.call(this,name)
}
inheritPrototype(Son,Father)
Son.prototype.sayAge = function(){
console.log(this.age)
}
var s1 = new Son("二儿子",16)
s1.sayAge()
console.log(s1)
//寄生组合式的继承能使 子类原型继承父类原型,子类构造函数继承父类构造函数的属性方法
那么问题来了,为什么不可以 Son.prototype = Father.prototype 来实现子类原型继承父类原型,好吧,我们来试试~
function Father(name){
this.name = name
this.colors = ["red","yellow"]
}
Father.prototype.sayName = function(){
console.log(this.name)
}
function Son(name,age){
this.age = age
Father.call(this,name)
}
Son.prototype = Father.prototype
Son.prototype.constructor = Son
Son.prototype.sayAge = function(){
console.log(this.age)
}
var s1 = new Son("张强",27)
console.log(s1)
s1.sayName()
var f1 = new Father("冯博志")
console.log(f1)
咋一看好像可以实现继承,但是子类原型上的属性方法影响到父类的原型方法,这里就涉及到js的浅拷贝问题,当一个改变另一个随之改变,所以,这种方法是不可取的。
总结:
js实现继承的方法主要有这些,但主要的是组合方式和寄生组合方式,使用继承能使用父类的属性和方法,增加代码的可复用性。
js继承的方式的更多相关文章
- js继承的方式及其优缺点
js继承方法 前因:ECMAScript不支持接口继承,只支持实现继承 一.原型链 概念:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针,让 ...
- JS继承 实现方式
JS中继承方式的实现有多种方法,下面是比较推荐的方法,其它继承方式可做了解: function object(o) { function F() {} F.prototype = o; return ...
- js 继承的方式
//定义object的继承方法 Object.extend = function(destination, source) { for(property in source) { destinatio ...
- js面向对象编程(第2版)——js继承多种方式
附带书籍地址: js面向对象编程(第2版)
- JS继承的原理、方式和应用
概要: 一.继承的原理 二.继承的几种方式 三.继承的应用场景 什么是继承? 继承:子类可以使用父类的所有功能,并且对这些功能进行扩展.继承的过程,就是从一般到特殊的过程.要了解JS继承必须首先要了解 ...
- JS类继承常用方式发展史
JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...
- JS继承以及继承的几种实现方式总结
传统面向对象语言:继承是类与类之间的关系. 而在js中由于es6之前没有类的概念,所以继承是对象与对象之间的关系. 在js中,继承就是指使一个对象有权去访问另一个对象的能力. 比如:比如对象a能够访问 ...
- js 继承的几种方式
JS继承的实现方式: 既然要实现继承,那么首先我们得有一个父类,代码如下: function Animal(name) { // 属性 this.name = name || '小白'; // 实例方 ...
- js对象的几种创建方式和js实现继承的方式[转]
一.js对象的创建方式 1. 使用Object构造函数来创建一个对象,下面代码创建了一个person对象,并用两种方式打印出了Name的属性值. var person = new Object(); ...
随机推荐
- HDU 5416
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory ...
- java Bean及其使用
1 getter/setter方法 java例子: public class student { private String name; public String getName() { retu ...
- wxpc
- python js
js = 'var a=document.getElementsByClassName("user-data-right")[0];a.target="_self&quo ...
- SpringMVC_1
//@SessionAttributes(value={"user"},types={String.class}) @Controller public class SpringM ...
- bzoj5311: 贞鱼
还是年轻啊算的时候少乘一个4000被卡二分上界了...%%%%bright教我超级快速读D飞bzoj垃圾卡常数据 我们容易写出这样的DP方程:f[i][j]=f[k][j-1]+val(k+1,j) ...
- ZOJ 1871:Steps
Steps Time Limit: 2 Seconds Memory Limit: 65536 KB One steps through integer points of the stra ...
- RDA UMF进程 & UMF_IR.C 遥控处理
SIS架构图: SW Structure APP Event Flow :消息分发流程 UMF进程: int umf_main(int argc, char* argv[]) { umf_Init() ...
- nginx开发(一) 源码-编译
1:获取源码 http://nginx.org/download/nginx-1.8.0.tar.gz 2:编译 解压之后,进入根目录,执行 ./configuer.sh make make inst ...
- libpcap 中调用ctime()时警告提示:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] ...