作者: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. java方法:flush()

    flush本意是冲刷,这个方法大概取自它引申义冲马桶的意思,马桶有个池子,你往里面扔东西,会暂时保存在池子里,只有你放水冲下去,东西才会进入下水道. 同理很多流都有一个这样的池子,专业术语叫缓冲区,当 ...

  2. Spring Boot 系列教程2-Data JPA

    Spring Data JPA 用来简化创建 JPA 数据访问层和跨存储的持久层功能. 官网文档连接 http://docs.spring.io/spring-data/jpa/docs/curren ...

  3. servlet & javabean

    1.servelet 什么是Servlet?① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③这个在服务器端运行,用以处理客户端的请求 Servlet相关包的 ...

  4. 4位开锁<dfs>

    题意: 有一个四位密码的锁,每一位是1~9的密码,1跟9相连.并且相邻的连个密码位可以交换.每改变一位耗时1s,给出锁的当前状态和密码,求最少解锁时间. 思路: 用bfs枚举出所有相邻交换的情况,并记 ...

  5. jquery 控制文本输入的字数

    <textarea maxlength="200" id="remarks" onkeyup="title_len();">&l ...

  6. struts2获得提交是get还是post方法提交

    String method=ServletActionContext.getRequest().getMethod(); System.out.println(method); 如果是get  会打印 ...

  7. ViewPager和View组合 实现页面的切换

    //--------------主页面------------------------------- package com.bw.test; import java.util.ArrayList;i ...

  8. synchronized关键字以及实例锁 类锁

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...

  9. (一)、Struts第一天

    (一).Struts第一天 1. JavaWeb知识回顾 n 客户端编程 HTLM/CSS/JS n XML技术 会写XML * 基本语法 * DTD * Schema 会读XML * Dom4J读取 ...

  10. 【转】调用getActionBar()报Call requires API level 11 (current min is 8): android.app.Activity#getActionBar

    解决办法: 第一种方法:修改AndroidManifest.xml中的minSdkVersion=11   第二种方法: 1.导入android-support-v7-appcompat项目,并将其作 ...