js面向对象继承_构造函数继承与es6中的extends继承

news/2024/7/11 1:51:36 标签: javascript, html5, es6

作用

主要作用是实现方法和属性的复用

方法一 父类构造函数法

通过调用父类的构造函数来使得子类继承父类的属性和方法
缺点 => 只 继承了 父类实例的相关属性和方法 原型对象上的属性和方法没有继承

javascript">function Cat(name, age) {
  this.name = name;
  this.age = age;
}

Cat.prototype.say = function () {
  console.log("喵喵喵");
}

function blueCat(name, age) {
  //Cat()   如果直接这样调用,则构造函数里面的this将会指向window
  Cat.call(this, name, age);
  //通过call函数强制将构造函数Cat的this指向blueCat创建的实例化对象
}

let tom = new blueCat("Tom", "3");
console.dir(tom);

在这里插入图片描述
上面案例便是通过调用父类构造函数来继承父类的属性和方法,由于构造函数里面的this总是指向该构造函数所创建的实例化对象,所以子类通过call方法将父类的this也指向子类的实例化对象,但实例化子类对象时发现say()这个定义在Cat原型对象里面的方法并未被继承到

方法二 父类原型链赋值法

通过将父类的原型链复制到子类上进行继承。
缺点 => 引用同一个内存空间 只要有一个改动 另一个也会被修改

javascript">  function Cat(name, age) {
      this.name = name;
      this.age = age;
  }
  Cat.prototype.say = function () {
      console.log("喵喵喵");
  }
  let haha = new Cat("HaHa", "1");


  function blueCat(name, age, area) {
      this.area = area;
  }

  //将父类的原型链浅复制给子类
  blueCat.prototype = Cat.prototype;
  blueCat.prototype.say = function () {
      console.log("Playing the piano");
  }


  let tom = new blueCat("Tom", "3", "USA");
  blueCat.prototype.say();
  Cat.prototype.say();

在这里插入图片描述
当改变blueCat对象的say方法时,cat对象也会改变。

方法三 父类实例对象原型链法

为了补足方法二的缺陷,方法三将原本父类原型链直接复制给子类变成,创建父类实例化对象后,再将实例化的对象的原型链复制给子类,这样子类改变原型对象里面的方法时(有点方法重写的意思)就不会影响父类了。

javascript"> function Cat(name, age) {
     this.name = name;
     this.age = age;
 }
 Cat.prototype.say = function () {
     console.log("喵喵喵");
 }
 let catObj = new Cat("haha", "2");


 function blueCat(area) {
     this.area = area;
 }
 blueCat.prototype = catObj;
 blueCat.prototype.say = function () {
     console.log("Playing the piano");
 }
 let tom = new blueCat("USA");

 blueCat.prototype.say();
 Cat.prototype.say();

在这里插入图片描述
但是这样还是有问题,只能继承父类 原型对象上的属性和方法 => 实例的相关属性和方法无法继承,但是实例的属性放到了原型对象上。
在这里插入图片描述
可以看到子类blueCat没有继承到name和age反而将其放到了原型对象上了。

最终版 中间件法

通过拓宽原型链,达到继承父类原型对象方法的目的 (用父类实例化对象 覆盖子类的原型对象 => 要原型链)

javascript">
        function Cat(name, age) {
            this.name = name;
            this.age = age;
        }
        Cat.prototype.say = function () {
            console.log("喵喵喵");
        }
        let ha = new Cat("haha", "1");

        function blueCat(name, age, area) {
            //通过调用构造函数将实例化属性继承
            Cat(this, name, age);
            this.name = name;
            this.age = age;
            this.area = area;
        }

        //利用中间件做媒介,将原型对象继承给子类
        function Fn() { }
        Fn.prototype = Cat.prototype;

        blueCat.prototype = new Fn();
        //给子类对象强制定义constructor指向自己,否则实例化对象的constructor会和Fn里面的一样
        blueCat.prototype.constructor = blueCat;

        blueCat.prototype.say = function () {
            console.log("Playing the piano");
        }
        let tom = new blueCat("name", "2", "USA");

        console.dir(tom);

在这里插入图片描述

extends

以上的方法都是es5之前的继承方式,es6引入了class模块。如同java、c++、、、这类语言一样的类定义。

javascript">
        class Cat {
            constructor(name, age) {
                this.name = name;
                this.age = age;
            }
        }

        class blueCat extends Cat {
            constructor(name, age, area) {
                super(name, age, area);
                this.name = name;
                this.age = age;
                this.area = area;
            }

            say() {
                console.log("喵喵喵");
            }
        }
        let tom = new blueCat("Tom", "2", "USA");
        console.log(tom);

在这里插入图片描述
这样使得继承变得更为简单


http://www.niftyadmin.cn/n/1570100.html

相关文章

python socket_python接收syslog日志(WAF)并保存到数据库socket+MySQLdb+mysql

这个星期护网,大半夜值班又无聊又要盯着waf看,关键是waf隔一段时间就要重新登录一次,这谁能受得了啊!干脆接到自己电脑,直接看日志再分析可疑ip他不香吗!开始干!(代码在最后&#xf…

js中的数据驱动(基础)

利用数据驱动将数据之间的操作放到内存中&#xff0c;可以减少对页面元素的获取操作 对创建的对象进行监听其数据变化&#xff0c;每次有变化时更改页面元素的内容 <body><input type"text" id"price"><input type"text" id&qu…

闭包中的柯里化理解

闭包的作用就不多做赘述了&#xff0c;百度一下就知道了。 柯里化的作用就是调用了数据后立马释放 function fn() {let num 0;return function () {num;console.log(num);}}let result fn();result();result();console.log("------------------------------------------…

3dmax中摄影灯参数_VRay灯光其它参数及打灯光三步法则

我们再来看一下VR的其他参数比如在VRayIES里你会发现它有一个中止然后这里还有一个排除这些也算是Vray的一个通用参数&#xff0c;因为你会发现在Vray灯光里也有这样的参数它的意思也是一样的。所谓的排除就是把某些物体排除掉这个灯光的无论是阴影还是照明之外&#xff0c;就是…

闭包的使用

闭包的作用就不做赘述了&#xff0c;百度一下有很多。 下面演示一下作用 //css样式 /* <style>.textColorA {color: aqua;}.textColorB {color: red;}</style> */<body><button id"showA">显示A</button><button id"showB&qu…

android内容提供者_58、Android持久化存储之contentprovider内容提供者

概述ContentProvider虽说我们平时用的并不多&#xff0c;但是作为安卓四大组件之一&#xff0c;其地位不容忽视。ContentProvider的作用是为不同的应用之间数据共享&#xff0c;提供统一的接口&#xff0c;我们知道安卓系统中应用内部的数据是对外隔离的&#xff0c;要想让其它…

android透明色_类比于微信,如何对Apk进行极限压缩,谈下Android压缩8大步

面试官: 类比于微信&#xff0c;如何对Apk进行极限压缩,谈下Android压缩8大步心理分析&#xff1a;面试官是非常注重性能优化的&#xff0c;考求职者是否具备APK压缩技能。**求职者:**应该从每一步压缩开始&#xff0c;压缩的过程本质是挤牙膏的过程&#xff0c;一步一步挤。将…

vue的路由实现原理

主要代码 主要通过监听地址栏的hash变化结合动态组件来实现对应页面的切换 <template><div id"app"><h1>APP根组件</h1><hr /><a href"#/Home">Home</a><a href"#/Movie">Movie</a><…