call/apply的第一个参数如果为null。this指向window


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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM