jQuery: The Write Less, Do More JavaScript Library

Changeset 3298

Show
Ignore:
Timestamp:
09/15/07 02:24:44 (1 year ago)
Author:
jeresig
Message:

etooled the jQuery constructor, makes it work better for embedding (Bug #1585). Also took the opportunity to do some renaming in the constructor and init.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/core.js

    r3296 r3298  
    1414    var _jQuery = jQuery; 
    1515 
    16 var jQuery = window.jQuery = function(a,c) { 
    17     // If the context is global, return a new object 
    18     if ( window == this || !this.init ) 
    19         return new jQuery(a,c); 
    20      
    21     return this.init(a,c); 
     16var jQuery = window.jQuery = function(selector, context) { 
     17    // If the context is a namespace object, return a new object 
     18    return this instanceof jQuery ? 
     19        this.init(selector, context) : 
     20        new jQuery(selector, context); 
    2221}; 
    2322 
     
    3231 
    3332jQuery.fn = jQuery.prototype = { 
    34     init: function(a,c) { 
     33    init: function(selector, context) { 
    3534        // Make sure that a selection was provided 
    36         a = a || document; 
     35        selector = selector || document; 
    3736 
    3837        // Handle HTML strings 
    39         if ( typeof a  == "string" ) { 
    40             var m = quickExpr.exec(a); 
    41             if ( m && (m[1] || !c) ) { 
     38        if ( typeof selector  == "string" ) { 
     39            var m = quickExpr.exec(selector); 
     40            if ( m && (m[1] || !context) ) { 
    4241                // HANDLE: $(html) -> $(array) 
    4342                if ( m[1] ) 
    44                     a = jQuery.clean( [ m[1] ], c ); 
     43                    selector = jQuery.clean( [ m[1] ], context ); 
    4544 
    4645                // HANDLE: $("#id") 
     
    5150                        // by name instead of ID 
    5251                        if ( tmp.id != m[3] ) 
    53                             return jQuery().find( a ); 
     52                            return jQuery().find( selector ); 
    5453                        else { 
    5554                            this[0] = tmp; 
     
    5857                        } 
    5958                    else 
    60                         a = []; 
     59                        selector = []; 
    6160                } 
    6261 
    6362            // HANDLE: $(expr) 
    6463            } else 
    65                 return new jQuery( c ).find( a ); 
     64                return new jQuery( context ).find( selector ); 
    6665 
    6766        // HANDLE: $(function) 
    6867        // Shortcut for document ready 
    69         } else if ( jQuery.isFunction(a) ) 
    70             return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a ); 
     68        } else if ( jQuery.isFunction(selector) ) 
     69            return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( selector ); 
    7170 
    7271        return this.setArray( 
    7372            // HANDLE: $(array) 
    74             a.constructor == Array && a || 
     73            selector.constructor == Array && selector || 
    7574 
    7675            // HANDLE: $(arraylike) 
    7776            // Watch for when an array-like object is passed as the selector 
    78             (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a ) || 
     77            (selector.jquery || selector.length && selector != window && !selector.nodeType && selector[0] != undefined && selector[0].nodeType) && jQuery.makeArray( selector ) || 
    7978 
    8079            // HANDLE: $(*) 
    81             [ a ] ); 
     80            [ selector ] ); 
    8281    }, 
    8382