call/apply是用来改变函数的作用域的,第一次参数为this,第二个参数为传输的值,例如

    var a ="windowA";
    var b = "windowB";
    var str = "str";
    var myObject = {a:"myA",b:"myB"};
    function hello(s){
        alert("a= "+this.a + ", b= "+this.b+" "+s);
    }
    hello.call(null,str);//a ="windowA" b = "windowB" str
    hello.call(myObject,str);//a="myA" b="myB" str

 

如果第一个参数为null,则this指向window(在node环境中则指向global)

hello.call(null)//this 指向window
hello.call(window)//this同样指向window

 

相关文章:

  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-09
  • 2022-12-23
  • 2021-09-21
猜你喜欢
  • 2022-01-06
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案