Javascript 嵌套在直接函数中的函数工厂嵌套在 jQuery 准备好的函数中

Javascript function factory nested in an immediate function nested inside a jQuery ready

在这个模式中,有一个立即函数包装在一个 jQuery 就绪的函数中。

    $((function(_this) {
      return function() {
        document.write('called!');
      };
    })(this));

我不明白 returned 函数分配给了什么以及它是如何调用的。

http://codepen.io/florian/pen/OyLoRd

根据我对函数工厂的理解,你需要将它们分配给一个变量,但这里我不知道return将被分配到哪里。

jQuery.ready() 函数中是否有我不知道的特殊内容?

谢谢!

您将即时函数与 jQuery 就绪事件回调混淆了。

一般情况下,当你想在文档就绪时执行代码,你会这样做

$(function() {
  // code
});

您的代码的最终结果类似于以下代码:

$(function() {
    document.write('called!');
});

这是一个细分:

var func = function(_this) {
    return function() {
        document.write('called!');
    };
};

var onReady = func(this); // In your code, the declation of func and its exection are done together

// At this point onReady is equal to the inner function
// function() {
//    document.write('called!');
// }

$(onReady);