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行代码