zoukankan      html  css  js  c++  java
  • 继承

    // 原型链
    1.### 构造函数原型prototype构造函数通过原型分配的函数是所有对象所共享的。
    JavaScript 规定,每一个构造函数都有一个prototype 属性,指向另一个对象。注意这个prototype就是一个对象,
    这个对象的所有属性和方法,都会被构造函数所拥有。
    我们可以把那些不变的方法,直接定义在 prototype 对象上,这样所有对象的实例就可以共享这些方法。

    2.对象都会有一个属性 proto 指向构造函数的 prototype 原型对象,之所以我们对象可以使用构造函数 prototype
    原型对象的属性和方法,就是因为对象有 proto 原型的存在。

    3.对象原型( proto)和构造函数(prototype)原型对象里面都有一个属性 constructor 属性 ,constructor 我们称为构造函数,
    因为它指回构造函数本身。
    constructor 主要用于记录该对象引用于哪个构造函数,它可以让原型对象重新指向原来的构造函数。

    // 4.继承
    // A.call()可以调用函数

    • call()可以修改this的指向,使用call()的时候 参数一是修改后的this指向,参数2,参数3..使用逗号隔开连接
      function fn(x, y) {
      console.log(this);
      console.log(x + y);
      }
      var o = {
      name: 'andy'
      };
      fn.call(o, 1, 2);//调用了函数此时的this指向了对象o

    // B.子构造函数继承父构造函数中的属性
    先定义一个父构造函数
    再定义一个子构造函数
    子构造函数继承父构造函数的属性(使用call方法)
    // 1. 父构造函数
    function Father(uname, age) {
    // this 指向父构造函数的对象实例
    this.uname = uname;
    this.age = age;
    }
    // 2 .子构造函数
    function Son(uname, age, score) {
    // this 指向子构造函数的对象实例
    // 3.使用call方式实现子继承父的属性
    Father.call(this, uname, age); //调用father函数,指向son,使son实现继承使用父级的属性和方法
    this.score = score;
    }
    var son = new Son('刘德华', 18, 100);
    console.log(son);

    // C.借用原型对象继承方法
    先定义一个父构造函数
    再定义一个子构造函数
    子构造函数继承父构造函数的属性(使用call方法)
    // 1. 父构造函数
    function Father(uname, age) {
    // this 指向父构造函数的对象实例
    this.uname = uname;
    this.age = age;
    }
    Father.prototype.money = function() {
    console.log(100000);
    };
    // 2 .子构造函数
    function Son(uname, age, score) {
    // this 指向子构造函数的对象实例
    Father.call(this, uname, age);
    this.score = score;
    }
    // Son.prototype = Father.prototype; 这样直接赋值会有问题,如果修改了子原型对象,父原型对象也会跟着一起变化
    Son.prototype = new Father();
    // 如果利用对象的形式修改了原型对象,别忘了利用constructor 指回原来的构造函数
    Son.prototype.constructor = Son;
    // 这个是子构造函数专门的方法
    Son.prototype.exam = function() {
    console.log('孩子要考试');
    }
    var son = new Son('刘德华', 18, 100);
    console.log(son);

    // D.类的继承
    // 父类
    class Father{
    }
    // 子类继承父类
    class Son extends Father {
    }

    class Father {
        constructor(surname) {
          this.surname= surname;
        }
        say() {
          console.log('你的姓是' + this.surname);
         }
    }
    class Son extends Father{  // 这样子类就继承了父类的属性和方法
    }
    var damao= new Son('刘');
    damao.say();      //结果为 你的姓是刘
    
    //子类使用super关键字访问父类的方法
    //定义了父类
    class Father {
       constructor(x, y) {
       this.x = x;
       this.y = y;
       }
       sum() {
       console.log(this.x + this.y);
    	}
     }
    //子元素继承父类
        class Son extends Father {
            //子类如果使用constructor构造函数,必须添加super()关键字,调用父类构造函数
       		 constructor(x, y) {
        		super(x, y); //使用super调用了父类中的构造函数
        	}
        }
        var son = new Son(1, 2);
        son.sum(); //结果为3
  • 相关阅读:
    matlab中figure 创建图窗窗口
    matlab中imread 从图形文件读取图像
    matlab中imfinfo 有关图形文件的信息
    matlab中bitshift 将位移动指定位数
    matlab中reshape 重构数组
    matlab中find 查找非零元素的索引和值
    比特数
    matlab中fseek 移至文件中的指定位置
    poj 1039 Pipe(几何基础)
    poj 1556 The Doors(线段相交,最短路)
  • 原文地址:https://www.cnblogs.com/xm0328/p/13913685.html
Copyright © 2011-2022 走看看