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