console
function Parent() {
this.hobby = [
'running'
]
}
Parent.prototype.showHobby = function () {
console.log(this.hobby)
}
function Child(name) {
this.name = name
Parent.call(this)
}
Child.prototype = new Parent()
Child.prototype.showName = function () {
console.log(this.name)
}
Child.prototype.constructor = Child;
function myNew(fn, ...args) {
let obj = Object.create(fn.prototype)
let ret = fn.apply(obj, args)
return ret instanceof Object ? ret : obj
}
let c = new Child('zhang san')
let d = myNew(Child, 'zhang san')
console.log(c.__proto__)
console.log(d.__proto__)
c.showHobby()
d.showHobby()
c.showName()
d.showName()
手写js组合继承 + new操作符
body{
color: white
}