簡體   English   中英

jquery ready 函數如何允許它的第一個參數成為 jquery 對象?

[英]How does jquery ready function allow it's first parameter to become jquery object?

今天我偶然發現了這個:

jQuery(document).ready(function($$){
    console.log($$);
});

顯然, $$被認為是jQuery對象本身。 通常要做這樣的事情,我們需要傳遞jQuery變量:

(function($$){
    console.log($$);
}(jQuery))

但在准備好的函數的情況下,它如何知道$$jQuery對象? 我認為 jquery 以將它的第一個參數作為jQuery傳遞的方式在底層定義了 ready 函數。

在文檔中有所描述。

在這個情況下:

jQuery(document).ready(function($$){
    console.log($$);
});
  • 當 jQuery 腳本標簽運行時, window.jQuery將 jQuery 分配給它
  • jQuery(document)引用該全局標識符
  • jQuery.ready 的回調,按照設計,用 jQuery 對象調用它的回調

除非您使用jQuery.noConflict ,否則此功能沒有多大用處,但對於另一個類似的示例:

 theLibrary().ready(function($$){ console.log($$.libraryProp); });
 <script> // library example window.theLibrary = Object.assign( // the library is both a function () => ({ ready: (callback) => { callback(window.theLibrary); } }), // and an object with properties { libraryProp: 'libraryProp' } ); </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM