es5面向对象继承基本用法--笔记

184 阅读1分钟

es5 面向对象继承(个人理解概念继承,就是两个不相干的类,其中一个类想要使用另一个类的方法)

原型链继承可以继承构造函数上的属性和方法,也可以继承构造函数原型链的属性和方法!!
function Tabs(){
this.name = "小白";
thi.age = 60;
}
Tabs.prototype.fn1 = function(){
console.log("函数里面需要执行什么")
}

function GetInfo(){
}

GetInfo.prototype = new Tabs();

let  g = new GetInfo();
g.fn1();//打印结果  函数里面需要执行什么

--------------------------------------------------------
function Tabs(name,age){
this.name = name;
thi.age = age;
}
Tabs.prototype.fn1 = function(){
console.log(this.name,this.age)
}

function GetInfo(){
}

GetInfo.prototype = new Tabs();
let  g = new GetInfo();
g.fn1();//打印结果   undefined  undefined
上述  实例化子类的时候没法给父类传参!!!
--------------------------------------------------------
正确  !!原型链加对象冒充的组合继承模式  用call改变this指向
 
function Tabs(name,age){
    this.name = name;
    this.age = age;
}  
Tabs.prototype.fn1=function(){
console.log(this.name,this.age)
}

function Web(name,age){
Tabs.call(this,name,age)
} 

Web.prototype = new Tabs();

let  u =  new Web("小白",900);

u.fn1()//打印结果   小白  900


注:面向对象 es5 new 了之后能用的方法叫 实例方法