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实例想用怎么办?没法呀
看下一篇

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 20- 枚举,枚举原始值,枚举相关值,switch提取枚举关联值 Swift枚举: Swift中的枚举比OC中的枚...
    iOS_恒仔阅读 6,794评论 1 6
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 7,916评论 2 9
  • 8月22日-----字符串相关 2-3 个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消...
    future_d180阅读 4,540评论 0 1
  • 写在前面的话 代码中的# > 表示的是输出结果 输入 使用input()函数 用法 注意input函数输出的均是字...
    FlyingLittlePG阅读 8,127评论 0 9
  • 将Student构造函数的原型对象改为Person构造函数的原型对象注意点: 要想使用Person原型对象中的属性...
    仰望_IT阅读 1,553评论 0 1