撸撸jquery2.0.3源码-----前戏(精华)
撸撸jquery2.0.3源码
面试被哔哔没读过源码,一怒之下就开始读起来了,看看别人的博客,找找思路,自己也开始撸起了源代码;
var // 根元素(document) rootjQuery, _jQuery = window.jQuery, _$ = window.$; jQuery = function( selector, context ) { // 直接返回new 好的对象,不用显式的调用new jQuery(); return new jQuery.fn.init( selector, context, rootjQuery ); }, jQuery.fn = jQuery.prototype = { constructor: jQuery, //防止被外部篡改constructor init: function(){ return this;// } } jQuery.fn.init.prototype = jQuery.fn;//注意注意注意
这里的 jQuery.fn.init.prototype = jQuery.fn是精髓: 因为jQuery()返回的是jquery.prototype.init的实例.init 返回this已经是init函数本事,已经和jQuery分离了,这个时候就得把jQuery的原型(fn)赋值给jQuery.prototype.init.prototype;(有点像乱伦ヾ(◍°∇°◍)ノ゙);


浙公网安备 33010602011771号