最近写前段的代码比较多,jQuery是用的最多的一个对象,但是之前几次看了源码,都没搞清楚jQuery是怎么定义的,今天终于看明白怎么回事了。记录下来,算是一个新的开始吧。

(文中源码都是jQuery-1.10.2版本的)

先上一段jQuery定义的源码,定义了jQuery为一个function

     // Define a local copy of jQuery
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
}

这就是我们常用的格式:$("#div1");就是通过这个函数定义的

这个函数只有一行有效代码,就是实例化了一个类型,这个类型是在jQuery的原型中定义的,那么继续往下看。

 jQuery.fn = jQuery.prototype = {
// The current version of jQuery being used
jquery: core_version, constructor: jQuery,
init: function( selector, context, rootjQuery ) {
var match, elem; // HANDLE: $(""), $(null), $(undefined), $(false)
if ( !selector ) {
return this;
} // Handle HTML strings
if ( typeof selector === "string" ) {
if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
// Assume that strings that start and end with <> are HTML and skip the regex check
match = [ null, selector, null ]; } else {
match = rquickExpr.exec( selector );
} // Match html or make sure no context is specified for #id
if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array)
if ( match[1] ) {
context = context instanceof jQuery ? context[0] : context; // scripts is true for back-compat
jQuery.merge( this, jQuery.parseHTML(
match[1],
context && context.nodeType ? context.ownerDocument || context : document,
true
) ); // HANDLE: $(html, props)
if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
for ( match in context ) {
// Properties of context are called as methods if possible
if ( jQuery.isFunction( this[ match ] ) ) {
this[ match ]( context[ match ] ); // ...and otherwise set as attributes
} else {
this.attr( match, context[ match ] );
}
}
} return this; // HANDLE: $(#id)
} else {
elem = document.getElementById( match[2] ); // Check parentNode to catch when Blackberry 4.6 returns
// nodes that are no longer in the document #6963
if ( elem && elem.parentNode ) {
// Handle the case where IE and Opera return items
// by name instead of ID
if ( elem.id !== match[2] ) {
return rootjQuery.find( selector );
} // Otherwise, we inject the element directly into the jQuery object
this.length = 1;
this[0] = elem;
} this.context = document;
this.selector = selector;
return this;
} // HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) {
return ( context || rootjQuery ).find( selector ); // HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
return this.constructor( context ).find( selector );
} // HANDLE: $(DOMElement)
} else if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this; // HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
} if ( selector.selector !== undefined ) {
this.selector = selector.selector;
this.context = selector.context;
} return jQuery.makeArray( selector, this );
},
。。。其他方法省略
}

在上面这段代码中,定义了jQuery的原型,然后将原型赋给了jQuery.fn;$.fn这个东西大家应该都很熟悉了,我们扩展jQuery插件的时候时长用到它,其实就是想jQuery的原型中添加了方法。

在原型的定义中重要的就是init函数了,有三个参数:selector,context,rootjQuery。

在这个函数中根据各种情况做了判断,比如:selector对象为false时的就是处理$("")或者$(null)或者$(undefined),(注释上都有说明);

如果selector是function时,就是我们最常用的页面加载处理函数:$(function(){......});

到这里,应该就可以明白了,jQuery对象不过是一个函数,然后在内部实例化了一个jQuery.fn.init;

但是问题来了,我们做jQuery插件的时候,扩展的是jQuery.fn,是jQuery的原型,跟jQuery.fn.init的实例化对象没有关系,这是怎么回事?

接下来这行代码就解决了我的疑惑:

 // Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;

将jQuery原型的定义赋给了jQuery.fn.init的原型,这样我们在扩展jQuery原型的同时,也扩展了jQuery.fn.init的原型,那么jQuery对象就有了那些方法(确切的说应该是jQuery.fn.init类型的实例化对象)

接下来的源码定义了jQuery.extend=jQuery.fn.extend=function(){};然后通过这两个方法添加了预定义的方法和属性,比如:ready函数、isFunction函数、each函数等等

到这里差不多jQuery的定义就结束了,我们常用的一些方法也在源代码中通过extend方法预先定义好了。

jQuery源码分析之=>jQuery的定义的更多相关文章

  1. jQuery 源码分析3: jQuery.fn/ jQuery.prototype

    // 建立方法实例,提高方法访问的速度(避免在原型链上搜索) var deletedIds = []; var slice = deletedIds.slice; var concat = delet ...

  2. jQuery 源码分析4: jQuery.extend

    jQuery.extend是jQuery最重要的方法之一,下面看看jQuery是怎样实现扩展操作的 // 如果传入一个对象,这个对象的属性会被添加到jQuery对象中 // 如果传入两个或多个对象,所 ...

  3. jQuery 源码分析6: jQuery 基本静态方法(二)

    jQuery.extend({ // 遍历obj的所有值 // args 这参数只能内部调用的会用到 // 注意到,如果回调函数调用失败会直接跳出并中止遍历 // 当有args数组时,使用apply调 ...

  4. jQuery 源码分析5: jQuery 基本静态方法(一)

    jQuery在初始化过程中会为自己扩展一些基本的静态方法和属性,以下是jQuery 1.11.3版本 239 ~ 564行间所扩展的静态属性和方法   jQuery.extend({ // 为每个jQ ...

  5. jQuery 源码分析2: jQuery.fn.init

    //jQuery.fn.intit 中使用到的外部变量: // 判断是否为HTML标签或#id rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w ...

  6. 六.jQuery源码分析之jQuery原型属性和方法

    97 jQuery.fn = jQuery.prototype = { 98 constructor: jQuery, 99 init: function( selector, context, ro ...

  7. jQuery 源码分析 8: 回头看jQuery的构造器(jQuery.fn,jQury.prototype,jQuery.fn.init.prototype的分析)

    在第一篇jQuery源码分析中,简单分析了jQuery对象的构造过程,里面提到了jQuery.fn.jQuery.prototype.jQuery.fn.init.prototype的关系. 从代码中 ...

  8. jQuery源码分析-each函数

    本文部分截取自且行且思 jQuery.each方法用于遍历一个数组或对象,并对当前遍历的元素进行处理,在jQuery使用的频率非常大,下面就这个函数做了详细讲解: 复制代码代码 /*! * jQuer ...

  9. jQuery源码分析系列

    声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://git ...

随机推荐

  1. MySQL 学习用employee数据库表参考使用

    download place:https://launchpad.net/test-db/ ,choose this file from the right panel:employees_db-fu ...

  2. 机器学习编程语言之争,Python 夺魁【转载+整理】

    原文地址 en cn 本文内容 表现平平的 MATLAB 貌似强大的 Julia 本身无错的 R 语言 逐渐没落的 Perl 老而弥坚的 Python 我个人很喜欢 Python~ 随着科技的发展,拥 ...

  3. android实现qq邮箱多个图标效果

    前几天,蛋疼的技术主管非要实现类似装一个qq邮箱,然后能够使用qq邮箱日历的那么一个东西.相当于一个应用生成两个图标,可是不同的是点击不同的图标能够进入不同的应用,例如以下图的效果. 这效果百度了一天 ...

  4. 关于Asp.Net MVC 中 UpdateModel 的未能更新***模型的 解决方案!

    解决方案参考: http://blog.csdn.net/hudaijun/article/details/7293129 想法: 其实,不用UpdateModel,虽然笨些,但不会出什么古怪问题.当 ...

  5. SQL Server 几种锁的区别

    NOLOCK(不加锁)    此选项被选中时,SQL  Server  在读取或修改数据时不加任何锁.  在这种情况下,用户有可能读取到未完成事务(Uncommited  Transaction)或回 ...

  6. DataGridView中添加CheckBox列用于选择行

    DataGridView中添加CheckBox列用于选择行 1,编辑DataGridView,添加一列 CheckBox ,Name 赋值为 "select",如下图: 2,取消 ...

  7. HTML5+javascript实现图片加载进度动画效果

    在网上找资料的时候,看到网上有图片加载进度的效果,手痒就自己也写了一个. 图片加载完后,隐藏loading效果. 想看加载效果,请ctrel+F5强制刷新或者清理缓存. 效果预览:   0%   // ...

  8. java-cef系列视频第一集:从官方代码编译

    本视频介绍了如何从官方给出步骤编译java-cef代码,生成可运行可移植的发行版. 值得一提的是:截至2016-09-24java-cef代码编译方式有所改变,读者请自行查看bitbucket上关于编 ...

  9. asp.net首页设置

    在web.config中设置首页 <configuration> <system.web> <compilation debug="true" tar ...

  10. assets中放入中文文件名导致Android Studio编译错误

    一个android项目突然出现编译错误,如下: :app:processDebugResources FAILED FAILURE: Build failed with an exception. * ...