jQuery.core_01

自己读jQuery代码有些日子了,一直感觉没领悟到什么,一直也没有写些东东,来记录下自己的感悟,看到各位大神的blogs,就一直没勇气去写下来,纠结再三,觉得还是有必要写下点东西,不论幼稚,肤浅,只求自己有点长进...

 先看一下jQuery实例:

 

1 jQuery = function( selector, context ){
2   return new jQuery.fn.init( selector, context );  
3 }
4 
5 vae init = jQuery.fn.init = function( selector, context ){
6 
7 }

看到没,其实jQuery的构造函数是 init 

那么,jQuery实例就现在来看,是无法访问到jQuery.prototype的method,property.

再看一下,神奇的一段代码:

init.prototype = jQuery.prototype

这是一段测试编码:

var init = function( name, age ){
    this.name = name;
    this.age = age;
}

var jQuery = function( name, age ){
    return new init( name, age );
};

jQuery.prototype  = {
    getName: function(){
        return this.name;
    },
    
    getAge: function(){
        return this.age;
    },
    
    age: 19
};

init.prototype = jQuery.prototype;

var j = jQuery( 'branches',18);
console.log( j.age );
console.log( j.getName() );

jQuery.s = "static_";

jQuery.getName = function(){
    return this.s;
};

console.log( jQuery.getName() );

 

posted @ 2015-10-11 22:49  Olive_branch  阅读(99)  评论(0编辑  收藏  举报