1. call() apply() bind()
    2. 作用:
    3. 1.都可以改变this的指向
    4. 2.call apply的作用可以直接调用函数,函数立即执行
    5. 3.call apply的区别在于参数 apply必须传入数据
    6. 4.bind 只会产生一个函数的副本,不会立即调用函数

    // 第一种使用 调用函数

    1. //以下都是一个函数的调用,没有改变this的指向
    2. function fn(){
    3. conosole.log("hello world")
    4. console.log(this)
    5. }
    6. fn();
    7. fn.call();
    8. fn.apply();
    9. var f1 = fn.bind(); //只会产生一个函数的副本
    10. f1()

    //第二种指向

    1. //call apply 在不改变this的指向的时候,call传一个普通值即可 apply 必须是一个数组
    2. function foo(n,m){
    3. console.log(n,m)
    4. console.log(this)
    5. }
    6. foo(10,20);
    7. foo.call(null,20,30)
    8. foo.call(null,[]20,30)

    //改变this的指向

    1. function foo(n,m){
    2. var num =this.a +this.b+n+m
    3. console.log(num)
    4. }
    5. var a=10;
    6. var b=20
    7. foo(30,40)
    8. foo.call({a:100,b:200},300,400)
    9. foo.apply({a:1000,b:2000},[3000,4000])

    使用apply借用别的对象的方法

    1. var max =Math.max(10,20,30,40,50,4)
    2. var nums=[100,200,300,4000,50]
    3. var res =Math.max.apply(123,nums)

    使用applyi转换类数组

    1. 类数组也叫伪数组,不是一个真正的数组 也就是说不是使用new+Array 创建出来的
    2. 不能使用数组的方法 pop push
    3. 但是类数组当中,也有length属性,也有索引值,可以用来循环遍历,也可以存储数据
    4. 类数组也是一个对象
    5. var obj ={1:'aaa',2:"bbb",3:'ccc',4:"nnn",length:4,b:123} //注意 在没添加length之前是一个对象,在添加length之后变成伪数组
    6. //类数组存储数据
    7. obj.aaa=200;
    8. var arr=[];
    9. Array.prototype.push.apply(arr,obj)
    10. console.log(arr) //只打印length 之前的数据