日期:2014-05-16 浏览次数:20548 次
var undefined = 1;
alert(undefined);
(function( window, undefined ) {
alert(undefined);
})(window);
(function( window ) {
alert(undefined);
})(window);
------解决方案--------------------
(function( $, undefined ) {
...
})(jQuery);
后者紧随function的(jQuery)是什么意思?它们都代表了什么?
jQuery是写好的框架的名称,但是在这儿是一个新建好的jQuery对象,看他的jquery.1.1源码,或许能明白一些。
    var jQuery = function (a, c) {
        // If the context is global, return a new object
        if (window == this)
            return new jQuery(a, c);
        // Make sure that a selection was provided
        a = a || document;
        // HANDLE: $(function)
        // Shortcut for document ready
        // Safari reports typeof on DOM NodeLists as a function
        if (jQuery.isFunction(a) && !a.nodeType && a[0] == undefined)
            return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ](a);
        // Handle HTML strings
        if (typeof a == "string") {
            var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
            a = m ?
                // HANDLE: $(html) -> $(array)
                jQuery.clean([ m[1] ]) :
                // HANDLE: $(expr)
                jQuery.find(a, c);
        }
        return this.setArray(
            // HANDLE: $(array)
            a.constructor == Array && a ||
                // HANDLE: $(arraylike)
                // Watch for when an array-like object is passed as the selector
                (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray(a) ||
                // HANDLE: $(*)
                [ a ]);
    };
    // 主要看这段:if (window == this)
            return new jQuery(a, c);