function Person(name,age){
this.name = name
this.age = age
this.hobby = ['唱歌','打游戏']
this.obj= {
a:1,
b:2
}
}
function Person2(){
this.hos = '123'
}
Person2.prototype = new Person('张三',18) //原型继承
let Per = new Person2() //构造函数继承
let Per1 = new Person2()
Per.hobby.push('旅游')
Per.obj.a=3
Per.obj.c=4
Per.name = '王五'
console.log(Per instanceof Person) //判断per是否person原型,返回true
console.log(Per)
console.log(Per1)
由此可见原型继承时引用类型的值,改变值时父类的值也会改变
call、apply继承
//call可以继承多个父类继承传递参数时一个一个的传递
function Boy(...name){
Person.call(this, ...name)
Person2.call(this, ...name)
}
//apply也可以继承多个类递参数时传递数组
function Goy(...name){
Person.apply(this, [...name])
Person2.call(this, [...name[[...name].length-1]].join())
}
let boys = new Boy('王五',33,'女')
let goys = new Goy('张三丰', 100, '男')
boys.hobby.push('1231321')
goys.name = 'goys'
console.log(boys)
console.log(goys)