美文网首页
12.继承方式二

12.继承方式二

作者: Fl_来看看 | 来源:发表于2019-06-06 13:58 被阅读0次
  • 为了解决下面链接的弊端:无法在new Student();设置name,age,say。
    10.继承方式一
function Person(myName, myAge) {
            // let per = new Object();
            // let this = per;
            // this = stu;
            this.name = myName; // stu.name = myName;
            this.age = myAge; // stu.age = myAge;
            this.say = function () { // stu.say = function () {}
                console.log(this.name, this.age);
            }
            // return this;
        }
        function Student(myName, myAge, myScore) {
            // let stu = new Object();
            // let this = stu;
            Person.call(this, myName, myAge); //  Person.call(stu);
            this.score = myScore;
            this.study = function () {
                console.log("day day up");
            }
            // return this;
        }
        let stu = new Student("ww", 19, 99);
        // stu.name = "zs";
        // stu.age = 18;
        // stu.score = 99;
        console.log(stu.score);
        stu.say();
        stu.study();
  • call作用就是将this传递过去,通过在别的函数用传递过去的this进行属性和方法的设置。
  • 继承体现在stu拥有person的属性
  • 构造函数+call实现继承
  • 构造函数是工厂函数的缩写,缩写体现在注释部分
  • Student具有Person的name,age属性了,通过call函数

还是有弊端:假如Person.prototype添加了新的方法,Student实例想用怎么办?没法呀
看下一篇

相关文章

  • 12.继承方式二

    为了解决下面链接的弊端:无法在new Student();设置name,age,say。10.继承方式一 call...

  • 13.JavaScript-继承方式三

    上一篇12.继承方式二有这么一句话 还是有弊端:假如Person.prototype添加了新的方法,Student...

  • 原型继承

    原型链的继承 1.第一种继承方式(原型链继承) 2.第二种继承方式(第二种继承方式) 3.第三种继承方式(组合继承)

  • Java基础 线程与并发库 Day41 2018-12-31

    12. 线程的几种实现方式? 实现方式 通过继承Thread的类实现一个线程 通过实现Runnale接口实现一个线...

  • 12.继承

    为什么使用继承 使用继承 编写父类 class Pet {//公共的属性和方法} 编写子类 class Dog e...

  • 前端 | JS | 面试中不得不知道的JS 继承的五种方式

    目录: JS中继承的概念 为什么要使用继承? 继承的第一种方式:原型链继承1 继承的第二种方式:原型链继承2 继承...

  • 继承与闭包

    继承 继承是指一个对象直接使用另一对象的属性和方法。 继承的方式一 继承方式二对象冒充: call,apply方式...

  • js的继承方式

    js的继承方式 一、原型链继承 原型继承的缺点: 二. 构造函数继承 构造函数继承的缺点: 三. 组合式继承 组合...

  • javascript继承的等价模块化写法

    写法一: 写法二: 写法三: 继承: 调用方式:

  • 2021-01-26

    实现多线程的方式到底有几种?1.如何创建多线程方式一:继承Thread类方式二:实现Runnable接口继承Thr...

网友评论

      本文标题:12.继承方式二

      本文链接:https://www.haomeiwen.com/subject/tuoyxctx.html