[英]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($$);
});
window.jQuery
將 jQuery 分配給它jQuery(document)
引用該全局標識符除非您使用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.