apply 方法 (Function) (JavaScript)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   java   使用   io   for   

轉載:http://msdn.microsoft.com/zh-cn/library/4zc42wh1(v=vs.94).aspx

 

寫在前面:與call方法類似

 

調用函數,並用指定對象替換函數的 this 值,同時用指定數組替換函數的參數。

apply([thisObj[,argArray]])

 

參數

thisObj

可選。 要用作 this 對象的對象。

argArray

可選。 要傳遞到函數的一組參數。

 

備忘

如果 argArray 不是有效對象,則會出現“應為對象”錯誤。

如果既未提供 argArray 也未提供 thisObj,則原始 this 對象將被用作 thisObj 且不會傳遞任何參數。

 

樣本

下面的代碼示範如何使用 apply 方法。

 1 function callMe(arg1, arg2){ 2   console.log(‘this value: ‘+ this); 3   for (var i in callMe.arguments) { 4     console.log(‘arguments: ‘+ callMe.arguments[i]) 5   } 6 } 7  8 callMe(1, 2); 9 // this value: [object Window]10 // arguments: 111 // arguments: 2 12 13 callMe.apply(‘a‘, [‘b‘, ‘c‘])14 // this value: a15 // arguments: b16 // arguments: c 

 

要求

在以下文檔模式中受支援:Quirks、Internet Explorer 6 標準模式、Internet Explorer 7 標準模式、Internet Explorer 8 標準模式、Internet Explorer 9 標準模式、Internet Explorer 10 標準模式和 Internet Explorer 11 標準模式。此外,也在市集應用(Windows 8 和 Windows Phone 8.1)中受支援。

 

apply 方法 (Function) (JavaScript)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.