美文网首页
call与apply

call与apply

作者: 何大必 | 来源:发表于2018-11-18 16:29 被阅读0次

首先明确一点,call与apply的作用是一样的,只是传参的形式有区别而已。

1、call与apply的区别

  • apply( )
    apply 方法传入两个参数:一个是作为函数上下文的对象,另外一个是作为函数参数所组成的数组。
var person= {
            name: 'lily'
        }

        function func(age, job) {
            console.log(this.name +' is '+age+' years old,and she is a '+job);
        }

        func.apply(person, [18, 'coder']);    //lilyis 18 years old,and she is a coder

可以看到,person是作为函数上下文的对象,函数 func 中 this 指向了 person这个对象。参数18和coder是放在数组中传入 func 函数,分别对应 func 参数的列表元素。

  • call( )
    call 方法第一个参数也是作为函数上下文的对象,但是后面传入的是一个参数列表,而不是单个数组。
func.call(person, 18, 'student');    //lily is 18 years old,and she is a student

对于什么时候该用什么方法,其实不用纠结。如果你的参数本来就存在一个数组中,那自然就用 apply,如果参数比较散乱相互之间没什么关联,就用 call。

2、call与apply的用法

  • 改变 this 指向
            var stu={
                name:'哈哈'
            }
            var name='呵呵';
            function test(){
                console.log(this.name);
                console.log(this)
            }
            test();
            test.call(stu);

结果:


image.png
  • 借用别的对象的方法
var Person1  = function () {
    this.name = 'dada';
}
var Person2 = function () {
    this.getname = function () {
        console.log(this.name);
    }
    Person1.call(this);//this指向Person2,结果相当于给Person2加了name属性
}
var person = new Person2();
person.getname();       // dada

从上面我们看到,Person2 实例化出来的对象 person 通过 getname 方法拿到了 Person1 中的 name。因为在 Person2 中,Person1.call(this) 的作用就是使用 Person1 对象代替 this 对象,那么 Person2 就有了 Person1 中的所有属性和方法了,相当于 Person2 继承了 Person1 的属性和方法。

  • 调用函数
    apply、call 方法都会使函数立即执行,因此它们也可以用来调用
function func() {
    console.log('哈哈哈');
}
func.call(); 

3、call 和 bind 的区别

在 EcmaScript5 中扩展了叫 bind 的方法,在低版本的 IE 中不兼容。它和 call 很相似,接受的参数有两部分,第一个参数是是作为函数上下文的对象,第二部分参数是个列表,可以接受多个参数。
它们之间的区别有以下两点。

  • bind的返回值是函数
 var obj = {
                name: 'dudu'
            }

            function func() {
                console.log(this)
                console.log(this.name);
            }

            var func1 = func.bind(obj);
            func1();//dudu

bind 方法不会立即执行,而是返回一个改变了上下文 this 后的函数。而原函数 func 中的 this 并没有被改变,依旧指向全局对象 window。

  • 参数的使用
function func(a, b, c) {
    console.log(a, b, c);
}
var func1 = func.bind(null,'dudu');

func('A', 'B', 'C');            // A B C
func1('A', 'B', 'C');           // dudu A B
func1('B', 'C');                // dudu B C
func.call(null, 'dudu','A','B','C');      // dudu A B

call 是把第二个及以后的参数作为 func 方法的实参传进去,而 func1 方法的实参实则是在 bind 中参数的基础上再往后排。
在低版本浏览器没有 bind 方法,我们也可以自己实现一个。

if (!Function.prototype.bind) {
        Function.prototype.bind = function () {
            var self = this,                        // 保存原函数
                context = [].shift.call(arguments), // 保存需要绑定的this上下文
                args = [].slice.call(arguments);    // 剩余的参数转为数组
            return function () {                    // 返回一个新函数
                self.apply(context,[].concat.call(args, [].slice.call(arguments)));
            }
        }
    }

相关文章

网友评论

      本文标题:call与apply

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