javaScript中的apply call bind

apply call 綁定新的this後當即執行,返回執行結果

Math.max.apply(null,[1,2,3]);

Math.max_ this_綁定爲_ null_,當即執行返回結果3java

apply 接受類數組參數、真實數組

bind綁定新的this,返回綁定新的 **this**的對象,在須要的時候執行

function f(y) { return this.x+y; }
var o={x:1};
var g=f.bind(o);//將f綁定新的this o,返回新的函數g
g(2);//輸出3

bind 傳入的參數也會綁定到this

相關文章
相關標籤/搜索