call 和 apply 是借用他人的函数实现自己到功能, 具体表现在改变 this 指向, 借用他人方法
而不同的地方是 call 是把实参按照形参的个数传入, 而 apply 传入的是一个数组 (argument)
写一个实例
- <script>
- function Person(name,age,sex){
- this.name = name ;
- this.age = age ;
- this.sex = sex;
- }
- function Student(name,age,sex,tel,grade){
- Person.call(this,name,age,sex);
- this.tel = tel;
- this.grade = grade ;
- }
- var student = new Student('sunny',123,'male',139,2017);
- </script>
Student 函数是没有 name ,age ,sex 的 this 指向的, 这边用 call 调用了 Person 方法, 使 Student 能赋值 name 等属性
call 和 apply 的很多使用或许我尚且不知, 暂且浅显记下
来源: http://www.bubuko.com/infodetail-2945182.html