下面常用方法的詳細使用請查看:http://www.cnblogs.com/moqiutao/p/4775725.html
1.$.noConflict()方法
語法:jQuery.noConflict(removeAll)
removeAll:布爾值。指示是否允許徹底將 jQuery 變量還原。
源碼:
var// Map over jQuery in case of overwrite_jQuery = window.jQuery,// Map over the $ in case of overwrite_$ = window.$;jQuery.noConflict = function( deep ) {if ( window.$ === jQuery ) {window.$ = _$;}if ( deep && window.jQuery === jQuery ) {window.jQuery = _jQuery;}return jQuery; };
?2.$.proxy()方法
// A global GUID counter for objectsguid: 1,// Bind a function to a context, optionally partially applying any// arguments.proxy: function( fn, context ) {var tmp, args, proxy;if ( typeof context === "string" ) {tmp = fn[ context ];context = fn;fn = tmp;}// Quick check to determine if target is callable, in the spec// this throws a TypeError, but we will just return undefined.if ( !jQuery.isFunction( fn ) ) {return undefined;}// Simulated bindargs = slice.call( arguments, 2 );proxy = function() {return fn.apply( context || this, args.concat( slice.call( arguments ) ) );};// Set the guid of unique handler to the same of original handler, so it can be removedproxy.guid = fn.guid = fn.guid || jQuery.guid++;return proxy;}
?3.jQuery.extend = jQuery.fn.extend
// 合并兩個或更多對象的屬性到第一個對象中,jQuery后續的大部分功能都通過該函數擴展// 通過jQuery.fn.extend擴展的函數,大部分都會調用通過jQuery.extend擴展的同名函數// 如果傳入兩個或多個對象,所有對象的屬性會被添加到第一個對象target// 如果只傳入一個對象,則將對象的屬性添加到jQuery對象中。// 用這種方式,我們可以為jQuery命名空間增加新的方法。可以用于編寫jQuery插件。// 如果不想改變傳入的對象,可以傳入一個空對象:$.extend({}, object1, object2);// 默認合并操作是不迭代的,即便target的某個屬性是對象或屬性,也會被完全覆蓋而不是合并// 第一個參數是true,則會迭代合并// 從object原型繼承的屬性會被拷貝// undefined值不會被拷貝// 因為性能原因,JavaScript自帶類型的屬性不會合并// jQuery.extend( target, [ object1 ], [ objectN ] )// jQuery.extend( [ deep ], target, object1, [ objectN ] ) jQuery.extend = jQuery.fn.extend = function() {var options, name, src, copy, copyIsArray, clone,target = arguments[0] || {},i = 1,length = arguments.length,deep = false;// Handle a deep copy situation// 如果第一個參數是boolean型,可能是深度拷貝if ( typeof target === "boolean" ) {deep = target;target = arguments[1] || {};// skip the boolean and the target// 跳過boolean和target,從第3個開始 i = 2;}// Handle case when target is a string or something (possible in deep copy)// target不是對象也不是函數,則強制設置為空對象if ( typeof target !== "object" && !jQuery.isFunction(target) ) {target = {};}// extend jQuery itself if only one argument is passed// 如果只傳入一個參數,則認為是對jQuery擴展if ( length === i ) {target = this;--i;}for ( ; i < length; i++ ) {// Only deal with non-null/undefined values// 只處理非空參數if ( (options = arguments[ i ]) != null ) {// Extend the base objectfor ( name in options ) {src = target[ name ];copy = options[ name ];// Prevent never-ending loop// 避免循環引用if ( target === copy ) {continue;}// Recurse if we're merging plain objects or arrays// 深度拷貝且值是純對象或數組,則遞歸if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {// 如果copy是數組if ( copyIsArray ) {copyIsArray = false;// clone為src的修正值 clone = src && jQuery.isArray(src) ? src : [];// 如果copy的是對象 } else {// clone為src的修正值 clone = src && jQuery.isPlainObject(src) ? src : {};}// Never move original objects, clone them// 遞歸調用jQuery.extend target[ name ] = jQuery.extend( deep, clone, copy );// Don't bring in undefined values// 不能拷貝空值 } else if ( copy !== undefined ) {target[ name ] = copy;}}}}// Return the modified object// 返回更改后的對象return target;};
?