JS 原型链

github 地址

https://github.com/SuYaru/Pre/tree/master/PostPre03

这两天主要是扎实原型链的知识

一、修改原型对象的 指向

   原来是自己的构造函数Student ,后来变为 Person

改变后 sayHi 不能访问

 

二、prototype 中的 _proto_ 指向哪里?

 某个构造函数的prototype

只要是对象,就有下划线原型

实例的_proto_ 指向Person.prototype ---> prototype 里也有 _proto_ ,指向 Object.prototype. Object.prototype 里的 _proto_ 是 Null

若原型指向改变了,应在改变指向后再添加方法

 

三、原型继承

最终都指向 Object

四、 借用构造函数继承

//借用构造函数:构造函数名字.call(当前对象,属性,属性,属性....);
    //解决了属性继承,并且值不重复的问题
    //缺陷:父级类别中的方法不能继承

    function Person(name, age, sex, weight) {
      this.name = name;
      this.age = age;
      this.sex = sex;
      this.weight = weight;
    }
    Person.prototype.sayHi = function () {
      console.log("您好");
    };
    function Student(name,age,sex,weight,score) {
      //借用构造函数
      Person.call(this,name,age,sex,weight);
      this.score = score;
    }
    var stu1 = new Student("小明",10,"男","10kg","100");
    console.log(stu1.name, stu1.age, stu1.sex, stu1.weight, stu1.score);

    var stu2 = new Student("小红",20,"女","20kg","120");
    console.log(stu2.name, stu2.age, stu2.sex, stu2.weight, stu2.score);

    var stu3 = new Student("小丽",30,"妖","30kg","130");
    console.log(stu3.name, stu3.age, stu3.sex, stu3.weight, stu3.score);

 

五、 组合继承=原型继承+构造函数继承

主要由 Person.call  和  Student.prototype=new Person(); 实现

//原型实现继承
    //借用构造函数实现继承
    //组合继承:原型继承+借用构造函数继承

    function Person(name,age,sex) {
      this.name=name;
      this.age=age;
      this.sex=sex;
    }
    Person.prototype.sayHi=function () {
      console.log("阿涅哈斯诶呦");
    };
    function Student(name,age,sex,score) {
      //借用构造函数:属性值重复的问题
      Person.call(this,name,age,sex);
      this.score=score;
    }
    //改变原型指向----继承
    Student.prototype=new Person();//不传值
    Student.prototype.eat=function () {
      console.log("吃东西");
    };
    var stu=new Student("小黑",20,"男","100分");
    console.log(stu.name,stu.age,stu.sex,stu.score);
    stu.sayHi();
    stu.eat();
    var stu2=new Student("小黑黑",200,"男人","1010分");
    console.log(stu2.name,stu2.age,stu2.sex,stu2.score);
    stu2.sayHi();
    stu2.eat();

    //属性和方法都被继承了

六、拷贝继承

通过 foreach 循环,将对象的属性或方法,保存到另一个对象中

对象存储的是地址,放在栈中。对象里的内容放在堆中。

这里的仅改变obj1 的地址,不算真正意义上的拷贝

function Person() {
    }
    Person.prototype.age=10;
    Person.prototype.sex="男";
    Person.prototype.height=100;
    Person.prototype.play=function () {
      console.log("玩的好开心");
    };
    var obj2={};
    //Person的构造中有原型prototype,prototype就是一个对象,那么里面,age,sex,height,play都是该对象中的属性或者方法

    for(var key in Person.prototype){
      obj2[key]=Person.prototype[key];
    }
    console.dir(obj2);
    obj2.play();

 

obj2 本身也有一块堆的存储空间,只是将obj1 堆里的内容复制到obj2 的堆上,所以是拷贝继承

七、函数复习

尽量使用表达式,不要使用 函数声明

//函数的角色:
    //函数的声明
    function f1() {
      console.log("我是函数");
    }
    f1();
    //函数表达式
    var ff=function () {
      console.log("我也是一个函数");
    };
    ff();

 

八、严格模式

函数之前 使用 "use strict";(指令只允许出现在脚本或函数的开头) 表示 严格模式

文档开头可以使用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN” "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

九、this 指向

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值