JQuery内核学习(一)

模拟JQuery的实现:

 

1、起源—原型继承

 

   

    var $ = jQuery = function(){}
    jQuery.fn = jQuery.prototype = {
        jquery: "1.3.2",    //原型属性
        size: function() {    //原型方法
            return this.length;
        }
    }

 

 

2、生命—返回实例

 

    ①、第一步

        

        var $ =jQuery = function(){
            return new jQuery();    //返回类的实例
        }
        jQuery.fn = jQuery.prototype = {
            jquery: "1.3.2",
            size: function() {
                return this.length;
            }
        }

 

    ②、第二步

       

       var $ =jQuery = function(){
           return jQuery.fn.init();    //调用原型方法init()
       }
       jQuery.fn = jQuery.prototype = {
           init : function(){    //在初始化原型方法中返回实例的引用
               return this;
           },
           jquery: "1.3.2",
           size: function() {
               return this.length;
           }
       }

  

3、学步—分隔作用域

 

    ①、第一步

 

        var $ =jQuery = function(){
            return jQuery.fn.init();
        }
        jQuery.fn = jQuery.prototype = {
            init : function(){
                this.length = 0;
                this.test = function(){
                    return this.length;
                }
                return this;
            },
            jquery: "1.3.2",
            length: 1,
            size: function() {
                return this.length;
            }
        }

        

    测试结果:

  

        alert( $().jquery );    //返回"1.3.2"
        alert( $().test() );     //返回0
        alert( $().size() );     //返回0

  

    ②、第二步

       

        var $ =jQuery = function(){
            return new jQuery.fn.init();
        }
        jQuery.fn = jQuery.prototype = {
            init : function(){
                this.length = 0;
                this.test = function(){
                    return this.length;
                }
                return this;
            },
            jquery: "1.3.2",
            length: 1,
            size: function() {
                return this.length;
            }
        }

  

        测试结果:

 

        alert( $().jquery );    //返回undefined
        alert( $().test() );     //返回0
        alert( $().size() );     //抛出异常

  

4、生长—跨域访问

       

        var $ =jQuery = function(){
            return new jQuery.fn.init();
        }
        jQuery.fn = jQuery.prototype = {
            init : function(){
                this.length = 0;
                this.test = function(){
                    return this.length;
                }
                return this;
            },
            jquery: "1.3.2",
            length: 1,
            size: function() {
                return this.length;
            }
        }
        jQuery.fn.init.prototype = jQuery.fn;     //使用jQuery的原型对象覆盖init的原型对象

  

      测试结果:

 

        alert( $().jquery );     //返回"1.3.2"
        alert( $().test() );      //返回0
        alert( $().size() );      //返回0

 

 

5、成熟—选择器

     var $ =jQuery = function(selector, context ){      //定义类
            return new jQuery.fn.init(selector, context );  //返回选择器的实例
        }
        jQuery.fn = jQuery.prototype = {          //jQuery类的原型对象
            init : function(selector, context){       //定义选择器构造器
                selector = selector || document;    //设置默认值为document
                context = context || document;     //设置默认值为document
                if ( selector.nodeType ) {              //如果选择符为节点对象
                    this[0] = selector;                      //把参数节点传递给实例对象的数组
                    this.length = 1;                          //并设置实例对象的length属性,定义包含元素个数
                    this.context = selector;              //设置实例的属性,返回选择范围
                    return this;                                 //返回当前实例
                }
                if ( typeof selector === "string" ) { //如果选择符是字符串
                    var e = context.getElementsByTagName(selector);  //获取指定名称的元素
                    for(var i = 0;i<e.length;i++){     //遍历元素集合,并把所有元素填入到当前实例数组中
                        this[i] = e[i];
                    }
                    this.length = e.length;               //设置实例的length属性,即定义包含元素的个数
                    this.context = context;              //设置实例的属性,返回选择范围
                    return this;                                //返回当前实例
                } else{
                    this.length = 0;                         //否则,设置实例的length属性值为0
                    this.context = context;              //设置实例的属性,返回选择范围
                    return this;                                //返回当前实例
                }
            },
            jquery: "1.3.2",
            size: function() {
                return this.length;
            }
        }
        jQuery.fn.init.prototype = jQuery.fn;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值