一 继承
- 让一个不具有某些功能或属性的类或对象,通过某些方式使用另一个具有这些功能或属性的类或对象的功能或属性
var obj1 = {
name:"amdin",
show:function(){
console.log(this.name)
}
}
var obj2={
name:"root"
}
obj1.show();
obj1.show.call(obj2);
- 构造函数继承(改变this指向继承)
- 简单方便
- 多继承
- 只能继承构造函数内部属性或方法,无法继承原型身上的属性或方法
function Parent(n, s){
this.name = n;
this.sex = s;
this.show = function(){
console.log(this.name + "====" + this.sex);
}
}
Parent.prototype.init=function(){
console.log("hello")
}
function Child(n,s){
Parent.call(this,n,s)
}
var p = new Parent("张三", "男")
p.show();
p.init();
var c = new Child("李四"