jQuery源码解析(整体架构)

平常我们使用jQuery时一般主要为以下两种情况:

$(selector).addClass() 
$.ajax(settings)

根据使用方式,可以推断出jQuery:

  • 是一个能创造出不同对象的函数;
  • 是一个拥有诸多方法的对象;
  • jQuery与$同为一个函数的函数名。

根据假设我们推断函数的大致形式:

var jQuery = $ = function(selector,context){
    return {}   //返回一个对象
}
jQuery.xxx = function(arguments){ 
    //do something
};

由于每次均要返回一个不同的对象,所以最容易想到的方法是根据构造函数new一个对象。

var jQuery = $ = function(selector,context){
    return new Function(selector,context)
}
jQuery.xxx = function(arguments){ 
    //do something
};

此时,应当考虑构造函数Function()存放的位置,将它放置到原型中,并且每次制作jQuery插件时都用jQuery.fn来简化:

var jQuery = $ = function(selector,context){
    return new jQuery.prototype.init(selector,context);
}
jQuery.prototype = jQuery.fn = {
     init:function(selector,content){
            //to do something
          }
}
jQuery.xxx = function(arguments){ 
    //do something
};


若以$(selector).function()来调用方法须将方法存储在jQuery.prototype中,但是此时通过new jQuery.prototype.init(selector,context)创建的实例对象与jQuery.prototype无任何关系。由此,须让jQuery.prototype为创建实例的原型对象,改为:

var jQuery = $ = function(selector,context){
    return new jQuery.prototype.init(selector,context);
}
jQuery.prototype = jQuery.fn = {
     init:function(selector,content){
            //to do something
          }
}
jQuery.prototype.init.prototype = jQuery.prototype; //新增
jQuery.xxx = function(arguments){ 
    //do something
};

现在就能向jQuery.prototype中添加方法来拓展jQuery的功能了!

var jQuery = $ = function(selector,context){
    return new jQuery.prototype.init(selector,context);
}
jQuery.prototype = jQuery.fn = {
     init:function(selector,content){
            //to do something
      },
     addclass:function(arguments){
           //to do something
     }
}
jQuery.prototype.init.prototype = jQuery.prototype; //新增
jQuery.xxx = function(arguments){ 
    //do something
};

这样做没什么问题,但是jQuery作为一个十分成熟的开源框架,如果人人都往jQuery.prototype/或是jQuery自身中添加新方法会给框架的整体管理带来很大的麻烦。因此需要一个添加方法的正确途径;

var jQuery = $ = function(selector,context){
    return new jQuery.prototype.init(selector,context);
};
jQuery.prototype = jQuery.fn = {
     init:function(selector,content){
            //to do something
      }
};
jQuery.prototype.init.prototype = jQuery.prototype; 
jQuery.extend = jQuery.fn.prototype.extend = function(obj){
      //   this.key = obj.key
};
jQuery.extend({
     ajax:function(arguments){ 
          //do something
};
jQuery.fn.extend({
       addclass:function(arguments){
           //to do something
     }
})

最后将jQuery用立即执行函数包裹起来,使jQuery融入开发环境中:

(function(window,undefined){
    var jQuery = $ = function(selector,context){
        return new jQuery.prototype.init(selector,context);
    };
    jQuery.prototype = jQuery.fn = {
         init:function(selector,content){
                //to do something
          }
    };
    jQuery.prototype.init.prototype = jQuery.prototype; 
    jQuery.extend = jQuery.fn.prototype.extend = function(obj){
          //   this.key = obj.key
    };
    jQuery.extend({
         ajax:function(arguments){ 
              //do something
    };
    jQuery.fn.extend({
           addclass:function(arguments){
               //to do something
         }
    })
})(window);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • jquery是前端常用的库,它的源码也是十分的精妙,我本着敬畏之心来分析它,jquery 代码未压缩总共有1100...
    GarenWang阅读 599评论 0 0
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,881评论 2 17
  • 今天,更新有点晚, 但是干货不怕晚, 接着昨天讲得讲, 把第一段讲的没讲完的继续讲解。 (function(){ ...
    web_无笙阅读 395评论 0 2
  • 1.jQuery是如何寻找到DOM或者很多方法 $符号选择标签,然后进行事件监听或者DOM操作 可以把$,看成一个...
    大淀桑浮不起來阅读 1,011评论 1 10
  • 相遇没有任何预期 余光一记 倾付所有的心意 我挥手推辞 不想伤一个人的痴 你总说一见钟情很美好 我相信日久生情才靠...
    易清幽阅读 365评论 1 8