js实现继承
许多oo语言都支持两种继承方式:接口继承和实现继承,接口继承只继承方法签名,而实现继承则继承实际的方法,如前所述,由于函数没有签名,在ECMAScript中无法实现接口继承,只支持实现继承(JavaScript高级程序设计)
结合网上大家的理解,总结了七种方法
1、通过原型链继承(子类的prototype为父类的实例)
写的时候一直不明白原型链继承的第二个问题,为什么不能传递参数,后来百度,结合两位大哥的解释,明白多了,直接上图
直接上代码有注释
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>手写继承
-原型链继承
</title
>
</head
>
<body
>
<script
>
function Father(sex
){
this.name
="quan"
this.do=["打游戏","刷抖音","等消息"]
this.sex
=sex
}
Father
.prototype
.sayHi=function(){
console
.log('Im '+this.name
)
}
function Son(sex
){
this.age
=21
}
Son
.prototype
=new Father()
Son
.prototype
.sayAge=function(){
console
.log('Im '+this.age
)
}
let instance1
=new Son()
instance1
.sayHi()
instance1
.sayAge()
let instance2
=new Son()
instance2
.do.push('看直播')
console
.log(instance1
.do)
console
.log(instance2
.do)
instance1
.name
="ddd"
instance1
.__proto__
.name
="fff"
console
.log(instance1
.name
)
console
.log(instance2
.name
)
let instance3
=new Son('男')
console
.log(instance3
.sex
)
</script
>
</body
>
</html
>
2、通过借用构造函数继承(在子类型构造函数的内部,调用超类型构造函数)
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>手写继承
-借用构造函数继承
</title
>
</head
>
<body
>
<script
>
function Father(sex
){
this.name
="quan"
this.do=["打游戏","刷抖音","等消息"]
this.sex
=sex
this.sayHi=function(){
console
.log('Im '+this.name
)
}
this.saySex=function(){
console
.log('Im '+this.sex
)
}
}
function Son(sex
){
Father
.call(this,sex
)
this.age
=21
this.sayAge=function(){
console
.log('Im '+this.age
)
}
}
let instance1
=new Son('男')
console
.log(instance1
.age
)
console
.log(instance1
.name
)
instance1
.sayHi()
instance1
.sayAge()
instance1
.saySex()
let instance2
=new Son()
instance2
.do.push("看直播")
console
.log(instance1
.do)
console
.log(instance2
.do)
</script
>
</body
>
</html
>
3、组合继承(前两种结合)
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>手写继承
-组合继承
</title
>
</head
>
<body
>
<script
>
function Father(sex
){
this.name
="quan"
this.do=["打游戏","刷抖音","等消息"]
this.sex
=sex
}
Father
.prototype
.sayHi=function(){
console
.log('Im '+this.name
)
}
function Son(age
,sex
){
Father
.call(this,sex
)
this.age
=age
}
console
.log(Son
.prototype
.constructor
)
Son
.prototype
=new Father()
console
.log(Son
.prototype
.constructor
)
Son
.prototype
.constructor
=Son
Son
.prototype
.sayAge=function(){
console
.log('Im '+this.age
)
}
let instance1
=new Son(21,'男')
console
.log(instance1
.age
)
console
.log(instance1
.name
)
console
.log(instance1
.sex
)
instance1
.sayHi()
instance1
.sayAge()
let instance2
=new Son(18,'女')
instance2
.sayAge()
console
.log(instance2
.sex
)
instance2
.do.push("看直播")
console
.log(instance1
.do)
console
.log(instance2
.do)
</script
>
</body
>
</html
>
4、原型式继承(拷贝继承)
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>手写继承
-原型式继承
</title
>
</head
>
<body
>
<script
>
let Father
={
name
:"quan",
do:["打游戏","刷抖音","等消息"]
}
function object(o
){
function F() {}
F.prototype
=o
return new F()
}
let Son1
=object(Father
)
let Son2
=object(Father
)
console
.log(Son1
)
Son1
.name
="dd"
console
.log(Son1
)
Son2
.do.push('看直播')
console
.log(Father
.do)
Son1
.__proto__
.name
="DD"
console
.log(Son2
.name
)
</script
>
</body
>
</html
>
5、寄生式继承(可以理解为在原型式继承的基础上新增一些函数或属性)
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>手写继承
-寄生式继承
</title
>
</head
>
<body
>
<script
>
let Father
={
name
:"quan",
do:["打游戏","刷抖音","等消息"]
}
function object(o
){
function F() {}
F.prototype
=o
return new F()
}
function other(original
){
let clone
=object(original
)
clone
.sayHi=function(){
console
.log('Im '+clone
.name
)
}
return clone
}
let Son1
=other(Father
)
let Son2
=other(Father
)
console
.log(Son1
)
Son1
.name
="dd"
console
.log(Son1
)
Son1
.sayHi()
Son2
.sayHi()
console
.log(Son1
.sayHi
===Son2
.sayHi
)
Son2
.do.push('看直播')
console
.log(Father
.do)
Son1
.__proto__
.name
="DD"
console
.log(Son2
.name
)
</script
>
</body
>
</html
>
6、寄生组合式继承(通过借用构造函数继承属性,原型链的混成形式来继承方法)
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>手写继承
-寄生组合式继承
</title
>
</head
>
<body
>
<script
>
function Father(sex
){
this.name
="quan"
this.do=["打游戏","刷抖音","等消息"]
this.sex
=sex
}
Father
.prototype
.sayHi=function(){
console
.log('Im '+this.name
)
}
function Son(age
,sex
){
Father
.call(this,sex
)
this.age
=age
}
function inheritPrototype(Son
,Father
){
let quan
=object(Father
.prototype
)
console
.log(quan
.constructor
)
quan
.constructor
=Son
console
.log(quan
.constructor
)
Son
.prototype
=quan
}
function object(o
){
function F() {}
F.prototype
=o
return new F()
}
inheritPrototype(Son
,Father
)
Son
.prototype
.sayAge=function(){
console
.log('Im '+this.age
)
}
let instance1
=new Son(21,'男')
console
.log(instance1
.age
)
console
.log(instance1
.name
)
console
.log(instance1
.sex
)
instance1
.sayHi()
instance1
.sayAge()
let instance2
=new Son(18,'女')
instance2
.sayAge()
console
.log(instance2
.sex
)
instance2
.do.push("看直播")
console
.log(instance1
.do)
console
.log(instance2
.do)
</script
>
</body
>
</html
>
7、ES6继承(实际也只是一个语法糖)
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>手写继承
-ES6继承
</title
>
</head
>
<body
>
<script
>
class Father{
constructor(sex
){
this.name
="quan"
this.do=["打游戏","刷抖音","等消息"]
this.sex
=sex
}
sayHi(){
console
.log('Im '+this.name
)
}
saySex(){
console
.log('Im '+this.sex
)
}
}
class Son extends Father{
constructor(age
,sex
){
super(sex
)
this.age
=age
}
sayAge(){
console
.log('Im '+this.age
)
}
}
let instance1
=new Son(21,'男')
console
.log(instance1
.age
)
console
.log(instance1
.name
)
console
.log(instance1
.sex
)
instance1
.sayHi()
instance1
.sayAge()
let instance2
=new Son(18,'女')
instance2
.sayAge()
console
.log(instance2
.sex
)
instance2
.do.push("看直播")
console
.log(instance1
.do)
console
.log(instance2
.do)
</script
>
</body
>
</html
>
参考文档
https://www.cnblogs.com/humin/p/4556820.html
作为自己学习过程的总结,菜鸡一枚,有什么问题与错误,请不吝赐教
转载请注明原文地址:https://tech.qufami.com/read-5864.html