构造函数和原型链

一. 使用构造函数创建对象:反复创建统一类型的多个对象
如何: 2步:
(1)定义构造函数
(2)用new操作符调用构造函数反复创建多个对象:
重点:new :
1.创建一个新的空对象;
2.将构造函数中的作用域赋给新对象(因此this就指向了这个新对象);
3.执行构造函数中的代码(为这个新对象添加属性);
4.返回新对象

//1. 定义构造函数,描述所有学生的统一结构
function Student(sname,sage){
//要求所有学生: 
  this.sname=sname;//必须有sname属性
  this.sage=sage;//必须有sage属性
  this.intr=function(){//必须有格式一致的intr方法
    console.log(`I'm ${this.sname}, I'm ${this.sage}`);
  }
}
var lilei=new Student("Li Lei",11);
var hmm=new Student("Han meimei",12);
console.log(lilei);
console.log(hmm);
lilei.intr();
hmm.intr();

二. 原型链
在这里插入图片描述
在这里插入图片描述
1.一旦创建函数,该函数就会有一个prototype属性,指向原型对象。
2.所有的原型对象都会自动获得一个constructor属性指向prototype属性所在的函数
3.构造函数通过new实例化一个对象
4.实例化对象有一个__proto__属性,指向构造函数的原型对象

function Student(sname,sage){ 
  this.sname=sname;
  this.sage=sage;
}
Student.prototype.intr=function(){
  console.log(`I'm ${this.sname}, I'm ${this.sage}`);
}
Student.prototype.className="初一2班";
var lilei=new Student("Li Lei",11);
var hmm=new Student("Han meimei",12);
console.log(lilei);
console.log(hmm);
lilei.intr();
hmm.intr();
console.log(
  lilei.__proto__==hmm.__proto__);//true
console.log(
  lilei.__proto__==Student.prototype
);//true

三.自定义继承:

  1. 单独修改一个对象的原型,而不影响其他对象的原型。
    -语法: 子对象.proto=新父对象
    -或 Object.setPrototypeOf(子对象,新父对象
function Student(sname,sage){
  this.sname=sname;
  this.sage=sage;
}/*prototype->{ }*/
var lilei=new Student("Li Lei",18);
var hmm=new Student("Han meimei",19);
var father={bal:100000000, car:"infiniti"}
hmm.__proto__=father;
//Object.setPrototypeOf(hmm,father);
console.log(lilei);
console.log(hmm);
console.log(lilei.bal,lilei.car);
console.log(hmm.bal,hmm.car);
  1. 统一修改一类对象的父对象。
    – 语法:构造函数.prototype=新父对象;
    – 时机: 必须在用构造函数创建子对象之前就修改
var father={bal:100000000, car:"infiniti"}

function Student(sname,sage){
  this.sname=sname;
  this.sage=sage;
}/*prototype->{ }*/

Student.prototype=father;

var lilei=new Student("Li Lei",18);
var hmm=new Student("Han meimei",19);
console.log(lilei);
console.log(hmm);
console.log(lilei.bal,lilei.car);
console.log(hmm.bal,hmm.car);

3.两种类型间的继承:如果发现多个类型之间拥有相同的属性结构和方法时,就要抽象出一个父类型
如何使用:
第一步:定义父类型
定义一个抽象父类型构造函数保存共有的属性
父类型的原型对象保存共有的方法
第二步:让子类型原型对象继承父类型原型对象
结果:保证子对象可调用抽象父类型原型对象中的方法
第三步:在子类型构造函数中借用父类型构造函数
父类型构造.call(this,参数)

//定义抽象父类型保存共有的属性和方法
function Enemy(fname,speed){
    this.fname=fname;
    this.speed=speed;
}
Enemy.prototype={
    fly:function(){
        console.log(`${this.fname} 以时速 ${this.speed} 飞行`);
    }
}
function Plane(fname, speed, score){
    Enemy.call(this,fname,speed);
    this.score=score;
}
Plane.prototype={
    getScore:function(){
        console.log(`击落${this.fname}得${this.score}分`)
    }
}
function Bee(fname,speed,award){
    Enemy.call(this,fname,speed);
    this.award=award;
}
Bee.prototype={
    getAward:function(){
        console.log(`击落${this.fname}奖励${this.award}`)
    }
}
Plane.prototype.__proto__=Enemy.prototype
Bee.prototype.__proto__=Enemy.prototype
var b16=new Bee('b16',2000,7)
b16.fly();
b16.getAward();
console.log(b16);
var f16=new Plane("F16", 1000, 5);
f16.fly();  //F16 以时速 1000 飞行
f16.getScore();  //击落F16 得 5 分
console.log(f16);

版权声明:本文为weixin_45696232原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。