12.继承方式二

  • 为了解决下面链接的弊端:无法在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实例想用怎么办?没法呀
看下一篇

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容