javaScript call与apply学习笔记

call和apply是借用他人的函数实现自己到功能,具体表现在改变this指向,借用他人方法

而不同的地方是call是把实参按照形参的个数传入,而apply传入的是一个数组(argument)

写一个实例

 1         <script>
 2             function Person(name,age,sex){
 3                 this.name = name ;
 4                 this.age = age ;
 5                 this.sex = sex;
 6             }
 7             function Student(name,age,sex,tel,grade){
 8                 Person.call(this,name,age,sex);
 9                 this.tel = tel;
10                 this.grade = grade ;
11             }
12             var student = new Student('sunny',123,'male',139,2017);
13         </script>

Student函数是没有name ,age ,sex的this指向的,这边用call调用了Person方法,使Student能赋值name等属性

call和apply的很多使用或许我尚且不知,暂且浅显记下

原文地址:https://www.cnblogs.com/raonet/p/10352520.html