美文网首页
call 应用实例

call 应用实例

作者: 灯火葳蕤234 | 来源:发表于2019-10-05 20:24 被阅读0次
    function Person(name, age, sex){
      this.name = name;
      this.age = age;
      this.sex = sex;
    }
    function Student(name, age, sex, tel, grade) {
      //var this = {}
      Person.call(this, name, age, sex);
      this.tel = tel;
      this.grade = grade;
    }
    var student = new Student('sunny', 123, 'male', 139, 2019);
    
    call 和 apply 的区别

    都是改变 this 指向的,但是传参列表不同
    call 需要把实参按照形参的个数传进去
    apply 需要传一个 arguments (把参数放到一个数组中)

    相关文章

      网友评论

          本文标题:call 应用实例

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