作者:zccst

首先,$(function(){})是$(document).ready(function(){})的简写形式。

在日常使用中,我们会把代码写到$(function(){})中,今天看看jQuery是如何做的(过程有点长)。

1,看jQuery入口,结论是$(function(){})是$(document).ready(function(){})的简写形式

$(function(){})相对于$()中传入了一个function类型的数据。根据源码:

jQuery.fn = jQuery.prototype = {

init:function( selector, context, rootjQuery ) {

if ( jQuery.isFunction( selector ) ) {
            return rootjQuery.ready( selector );
        }

}

}

而rootjQuery就是$(document),见866行源码

// All jQuery objects should point back to these
rootjQuery = jQuery(document);

2,$(document).ready(function(){})是如何实现的呢?

从$().ready()的调用方式可以看出,ready是对象方法,见240行源码

ready: function( fn ) {
    // Add the callback
    jQuery.ready.promise().done( fn );

return this;
},

可以看出,jQuery.ready.promise()是一个对象,调用了done(fn)方法,表明调用了一个延迟对象,再看一下jQuery.ready.promise()

 jQuery.ready.promise = function( obj ) {
if ( !readyList ) { readyList = jQuery.Deferred(); // Catch cases where $(document).ready() is called after the browser event has already occurred.
// we once tried to use readyState "interactive" here, but it caused issues like the one
// discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
if ( document.readyState === "complete" ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
setTimeout( jQuery.ready );//调用工具方法jQuery.ready } else { // Use the handy event callback
document.addEventListener( "DOMContentLoaded", completed, false ); // A fallback to window.onload, that will always work
window.addEventListener( "load", completed, false );//回调函数在90行,也调用工具方法jQuery.ready
}
}
return readyList.promise( obj );
};

随机推荐

  1. 这几天用到的 Linux 命令

    下面总结一下这几天用到的linux 命令,记录一下: 13 netstat -atunlp 26 apt-get install python-pip 27 pip install shadowsoc ...

  2. java中对List中对象排序实现

    package com.test; import java.util.ArrayList; import java.util.Collections; import java.util.Compara ...

  3. AutoTile 自动拼接(四) 学习与实践

    今天主要来说下,数据绑定. 之前第一章,我说到 把 资源图 画成格子,你们应该还有印象吧. 那么,当我 知道 格子数据,能否拿到 资源对应的图片呢? 大家先复习一下 第一章,发现很多格子数据 是相同的 ...

  4. StringWriter/PrintWriter在Java输出异常信息中的作用

    闲来无事,看看JUnit的源代码.刚刚开始看就发现一段有趣的代码: public String trace() { StringWriter stringWriter = new StringWrit ...

  5. Servlet与JSP的异同

    1.什么是Servlet A Java servlet is a Java programming language program that extends the capabilities of ...

  6. 解决Android Studio Gradle Build特别慢的问题

    解决Android Studio Gradle Build 特别慢的问题 C:\Users\你的用户名\.gradle目录下新建一个文件名为gradle.properties的文件.内容为:org.g ...

  7. linux创建vg、lv

    LVM磁盘管理 一.LVM简介... 1 二. LVM基本术语... 2 三. 安装LVM... 3 四. 创建和管理LVM... 4 2. 创建PV.. 6 3. 创建VG.. 7 4. 创建LV. ...

  8. 转 如何使用V7包中ActionBar(Eclipse版)

    http://blog.csdn.net/appte/article/details/11712591 以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlo ...

  9. MyEclipse报错 Building workspace has encountered a problem Errors occurred during the build 的2种解决方法

    1: Building workspace has encountered a problem Errors occurred during the build 如果报错这个 那么有可能是jar包,报 ...

  10. localStorage、sessionStorages 使用

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有 ...