2.3 能夠給jQuery添加靜態方法。 javascript
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write( + "<br />") document.write(arr) arr.push("James")
輸出:java
George,John,Thomas 4 George,John,Thomas,James
function funcB(a, b) {
funcA.call(testO, a, b);
}ajax
funcB(1,2); //this變成了testO數組
咱們定義funcB函數的中,調用了funcA的call函數,這個時候咱們改變了funcA中this的指向,本來指向window的,如今指向了call的第一個參數testO這個對象。並且調用call時,由於funcA函數有兩個參數,因此若是要想funcA傳遞參數,必須一一指出參數,即後面的兩個參數a和b,或者能夠只穿第一個參數app
即:funcA.call(testO);或者只傳a,即:funcA.call(testO,a);async
而apply與call的區別僅在於,apply的第二個參數能夠是數組形式,並且沒必要一一指出參數,funcA.apply(testO,[a,b])ide
<script type="text/javascript"> function testFuc() {
alert(this.color);
}函數
$(function () {
1.testFuc(); //彈出「透明」
2.testFuc(this); //彈出「undefined」
3.testFuc.call(this.parent); //彈出「透明」
4.testFuc.call(window); //彈出「透明」
5.testFuc.call(testObj); //彈出「紅色」
});
</script>post