用匿名函数包装的目的

Yan*_* Yi 0 javascript jquery anonymous-function google-chrome-extension

我正在构建chrome扩展,并在我的内容脚本中包含以下代码.pep是一个让事物变得可拖动的图书馆.有趣的是,我的代码适用于案例#2而不是#1.为什么会这样?

看起来第二种情况是使用匿名函数包装函数调用(虽然我不确定为什么之前需要jquery)

//1
$('#square').pep();

//2
$(function($) {
    $('#square').pep();
});
Run Code Online (Sandbox Code Playgroud)

jar*_*ack 6

第二种情况是jQuery的"on document ready"的简写.

它相当于:

$(document).ready(function() {
    $('#square').pep();
});
Run Code Online (Sandbox Code Playgroud)

文档:https://api.jquery.com/ready/