JQuery 소스 4/96-283

10690 단어
JQuery.fn = JQuery.prototype = {
    jquery: core_version,
    //  JQuery    
    constructor: jQuery,
    //       JQuery,       ,     new            ,           constructor        
    /*
    function A(){}
    var a=new A();
    a;// A __proto__:constructor:f A()
    */
    /*
                   :
    1. function A(){}
        A.prototype.init=function (){};
    2. function A(){}
        A.prototype={
            init:function(){}
        }
              ,        ,1       new    constructor     A,  2      A prototype           ,    A.prototype constructor            ,       ,          ,       Object,                constructor     Object,          constructor   JQuery,          
    */
    init: function (selector, context, rootJQuery) {
        //init jq          $          ,                     {0:div,1:div,length:2...}       
        //$() //jQuery.fn.init {}
        //            ,        this   ,  new JQuery.fn.init()     
        /*
            function $() {
                return new $.prototype.init();
            }
            $.prototype={
                init:function () {
                    console.log(this);
                    this.length=1;
                    return this;
                }
            }
            $(); //init {}
        */

        //selector        
        //context        
        //rootJQuery $(document)

        var match, elem;

        while(true){
            console.log("        ")
        }

        if (!selector) {
            //          ,         $(null),$(undefined),$(false)  ,    $
            return this;
        }

        if (typeof selector == "string") {
            //               
            //$("ele"),$("#id"),$(".class"),$("
  • ") if (selector.charAt(0) == "" && selector.length >= 3) { // $(
  • xx
  • ) math = [null, selector, null]; } else { // $("#id),$("
  • hi") match = rquickExpr.exec(selector) //exec js , , , //rquickExpr $("#id),$("
  • hi") , match=null //$(#id) match=['#id',null,'id] $(
  • hi) match=[
  • hi,
  • ,null] } if (match && (match[1] || !context)) { // , $(
  • xx
  • ),$("
  • hi"),$("#id") if (match[1]) { // $(
  • hi),$("
  • xxx
  • ") context = context instanceof JQuery ? context[0] : context; //jq , $(
  • ),$(
  • ,document),$(
  • ,$(document) // context ,context instanceof JQuery true $(document), context[0] document, context document jQuery.merge(this, JQuery.parseHTML( math[1], context && context.nodeType ? context.ownerDocument || context : document, // content ( ), , document // createElement document , //ownerDocument iframe , iframe , iframe document true )) //jQuery.parseHTML /* var str='
  • 1
  • 2
  • '; $.parseHTML(str); //[li,li] var match="
  • hi"; $.parseHTML(match); //[li] var march="
  • hi" $.parseHTML(match); //[li.a] */ // 3 ,1 ,2 (),3bool , false, $() script , DOM , true script , str='alert(1)<\/script>', , alert (\ , , ); //JQuery.merge() ( ), , , , /* var arr = ["a", "b"], obj = { 0: 'c', 1: "d", length: 2 }; $.merge(obj,arr) //{0: "c", 1: "d", 2: "a", 3: "b", length: 4} */ // jq , parseHTML if (rsingleTag.test(match[1]) && JQuery.isPlainObject(context)) { // $("<li>", {title: 'li'}) //jQuery.isPlainObject() , "{}" "new Object" for (match in context) { if (jQuery.isFunction(this[match])) { this[match](context[match]) } else { this.attr(match, context[match]) } // {} key , html, //$.isFunction($("li")["html"]) //true // jQuery value , // JQuery , attr key:value } } return this; } else { // $(#id) elem = document.getElementById(match[2]); if (elem && elem.parentNode) { // , 4.6 , , , , this.length = 1; this[0] = elem; } this.context = document; this.selector = selector; return this; } } else if (!context || context.jquery) { // $(expr),$(expr,$(xx)) ,context.jquery jq return (context || rootJQuery).find(selector); // !context $(document) // $(xx) , // jq , find() } else { // $(expr,document) return this.constructor(context).find(selector); //js constructor() //jq constructor(context) JQuery.fn.init(),context 0 /* $().constructor("button") jQuery.fn.init [button#more, prevObject: jQuery.fn.init(1), context: document, selector: "button"] */ } // $(".class"),$("ele") , find } else if (selector.nodeType) { // $(document) this.context = this[0] = selector; // this.length = 1; return this; } else if (JQuery.isFunction(selector)) { // $(function) return rootJQuery.ready(selector); // jq ready //so $(function(){}) $(document).ready(function(){}) } if (selector.selector !== undefined) { // $($(selector)) this.selector = selector.selector; this.context = this.context; } return jQuery.makeArray(selector, this); // $([]) }, selector: '', length: 0, toArray: function () { // //core_slice [].slice Array.prototype.slice return core_slice.call(this); }, get: function (num) { //get //$(xx).get() // $(xx) , //$(xx).get(-1) // $(xx) //$(xx).get(1) // $(xx) return num = null ? this.toArray() : (num < 0 ? this[this.length + num] : this[num]); }, pushStack: function (elems) { //* , jq , // $() , , // /* $("div").pushStack($("h3")).css("color","red"); */ var ret=JQuery.merge(this.constructor(),elems); //JQuery.fn.init {0:elems,length:1} ret.prevObject=this; ret.context=this.context; return ret; }, each:function (callback,args) { //$().each() $.each() return JQuery.each(this,callback,args); }, ready:function (fn) { //ready DOM , promise JQuery.ready.promise().done(fn); }, slice:function () { //slice $(xx) /* $(xx).slice(0,2) */ return this.pushStack(cose_slice.apply(this,arguments)); // pushStack , , apply // slice , pushStack JQuery /* var obj={ 0:'div', 1:'div', 2:'div', length:3 } console.log([].slice.apply(obj,[0,2])); //["div", "div"] apply,call ,bind */ }, first:function () { return this.eq(0); }, last:function () { return this.eq(-1) }, eq:function (i) { var len=this.length, j=+i+(i<0?len:0); return this.pushStack(i>=0&&j<len?[this[j]]:[]); }, map:function (callback) { return this.pushStack(JQuery.map(this,function (elem,i) { return callback.call(elem,i,elem) })) }, end:function () { return this.prevObject||this.constructor(null); //this.constructor(null) //JQuery.fn.init{} }, push:core_push, sort:[].sort, splice:[].splice } JQuery.fn.init.prototype=JQuery.fn; </code></pre> </article> </div> </div> </div> <!--PC WAP --> <div id="SOHUCS" sid="1385074255116455936"></div> <script type="text/javascript" src="/views/front/js/chanyan.js">
  • 좋은 웹페이지 즐겨찾기