标签:pre 原型链 ceo 有一个 对象 等于 tor name aop
function who(name){
this.name = name
this.say = function(){
console.log(this.name)
}
}
who.prototype.age = 10
function xiaoming(){
this.name = 'xiaoming'
}
xiaoming.prototype = new who()
let xiaoming1 = new xiaoming()
console.log(xiaoming1) //who()
console.log(xiaoming1.name) //xiaoming
console.log(xiaoming1.age) //10
console.log(xiaoming1 instanceof who) //true
重点:让新实例的原型等于父级的实例xiaoming.prototype = new who()
优点:
缺点:
function xiaohong(){
who.call(this,"xiaohong")
}
let xiaohong1 = new xiaohong()
console.log(xiaohong1) //who()
console.log(xiaohong1.name) //xiaohong
console.log(xiaohong1.age) //undefined
console.log(xiaohong1 instanceof who) //false
重点:使用call()
和apply()
的方法将父类函数继承过来
优点:
call()
和apply()
继承多个缺点:
function xiaopeng(name){
who.call(this,name)
}
xiaopeng.prototype = new who()
let xiaopeng1 = new xiaopeng('xiaopeng')
console.log(xiaopeng1.name) //xiaopeng
console.log(xiaopeng1.age) //10
console.log(xiaopeng1 instanceof who) //true
重点:使用了原型继承和构造函数继承
优点:
缺点:
function color(obj){
function fn(){}
fn.prototype = obj
return new fn
}
let red = new who()
let red1 = color(red)
red1.name = 'red'
console.log(red1.name) //red
console.log(red1.age) //10
console.log(red1 instanceof who) //true
重点:先封装一个函数,用来返回这个函数的调用,那么这个函数就变成一个可以随意添加属性的实例或对象
优点:类似于复制一个对象
缺点:所有的实例都会继承构造函数原型上的属性
function color(obj){
function fn(){}
fn.prototype = obj
return new fn
}
let red = new who()
//以上是原型式继承
function colorObject(obj){
let col = color(obj)
col.name = 'green'
return col
}
let red1 = colorObject(red)
console.log(red1.name) //green
console.log(red1.age) //10
console.log(red instanceof who) //true
console.log(typeof colorObject) //function
console.log(typeof red1) //object
重点:就是在原型式的外面再包一个函数体
优点:没有创建自定义类型,这个函数顺理成章就成了创建的新对象
缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法
function color(obj){
function fn(){}
fn.prototype = obj
return new fn
}
let red = color(who.prototype)
function blue(){
who.call(this)
}
blue.prototype = red
red.constructor = blue
let blue1 = new blue()
console.log(blue1.age) //10
重点:使用寄生继承和组合继承的方法
优点:
标签:pre 原型链 ceo 有一个 对象 等于 tor name aop
原文地址:https://www.cnblogs.com/kaizhadiyishuai/p/12386459.html