`
liu.da101
  • 浏览: 7810 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

jQuery源码解读2 - 整体架构

阅读更多
1,整体使用了一个匿名函数作为jQuery的命名空间,代码如下:

  (function(window,undefined){
    var jQuery = function(){};
    window.jQuery = window.$ = jQuery;
  })(window);
 

    最后,通过将jQuery对象暴露给window,从而可以在外部访问

2,接下来,看一下jQuery对象是怎样生成的

  第27行代码:
 
    return new jQuery.fn.init(selector,context,rootjQuery);
  

 
  这样就生成了一个对象,对象拥有的方法,都在jQuery.fn.init这个“类”里。而由于第324行代码将jQuery.fn.init的prototype指向了jQuery.fn,所以生成的对象的方法也可以在jQuery.fn这个“类”里。
 
  第324行代码:
 
    jQuery.fn.init.prototype = jQuery.fn;
  


  第100行代码:
 
    jQuery.fn = jQuery.prototype = {}
  

  将jQuery.fn又指向jQuery.prototype
 
  最终,使用jQuery("div")调用的函数是第102行代码
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics