jQuery常用的全局方法源码
生活随笔
收集整理的這篇文章主要介紹了
jQuery常用的全局方法源码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下面常用方法的詳細(xì)使用請(qǐng)查看:http://www.cnblogs.com/moqiutao/p/4775725.html
1.$.noConflict()方法
語(yǔ)法: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
// 合并兩個(gè)或更多對(duì)象的屬性到第一個(gè)對(duì)象中,jQuery后續(xù)的大部分功能都通過(guò)該函數(shù)擴(kuò)展// 通過(guò)jQuery.fn.extend擴(kuò)展的函數(shù),大部分都會(huì)調(diào)用通過(guò)jQuery.extend擴(kuò)展的同名函數(shù)// 如果傳入兩個(gè)或多個(gè)對(duì)象,所有對(duì)象的屬性會(huì)被添加到第一個(gè)對(duì)象target// 如果只傳入一個(gè)對(duì)象,則將對(duì)象的屬性添加到j(luò)Query對(duì)象中。// 用這種方式,我們可以為jQuery命名空間增加新的方法。可以用于編寫(xiě)jQuery插件。// 如果不想改變傳入的對(duì)象,可以傳入一個(gè)空對(duì)象:$.extend({}, object1, object2);// 默認(rèn)合并操作是不迭代的,即便target的某個(gè)屬性是對(duì)象或?qū)傩?#xff0c;也會(huì)被完全覆蓋而不是合并// 第一個(gè)參數(shù)是true,則會(huì)迭代合并// 從object原型繼承的屬性會(huì)被拷貝// undefined值不會(huì)被拷貝// 因?yàn)樾阅茉?#xff0c;JavaScript自帶類(lèi)型的屬性不會(huì)合并// 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// 如果第一個(gè)參數(shù)是boolean型,可能是深度拷貝if ( typeof target === "boolean" ) {deep = target;target = arguments[1] || {};// skip the boolean and the target// 跳過(guò)boolean和target,從第3個(gè)開(kāi)始 i = 2;}// Handle case when target is a string or something (possible in deep copy)// target不是對(duì)象也不是函數(shù),則強(qiáng)制設(shè)置為空對(duì)象if ( typeof target !== "object" && !jQuery.isFunction(target) ) {target = {};}// extend jQuery itself if only one argument is passed// 如果只傳入一個(gè)參數(shù),則認(rèn)為是對(duì)jQuery擴(kuò)展if ( length === i ) {target = this;--i;}for ( ; i < length; i++ ) {// Only deal with non-null/undefined values// 只處理非空參數(shù)if ( (options = arguments[ i ]) != null ) {// Extend the base objectfor ( name in options ) {src = target[ name ];copy = options[ name ];// Prevent never-ending loop// 避免循環(huán)引用if ( target === copy ) {continue;}// Recurse if we're merging plain objects or arrays// 深度拷貝且值是純對(duì)象或數(shù)組,則遞歸if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {// 如果copy是數(shù)組if ( copyIsArray ) {copyIsArray = false;// clone為src的修正值 clone = src && jQuery.isArray(src) ? src : [];// 如果copy的是對(duì)象 } else {// clone為src的修正值 clone = src && jQuery.isPlainObject(src) ? src : {};}// Never move original objects, clone them// 遞歸調(diào)用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// 返回更改后的對(duì)象return target;};?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的jQuery常用的全局方法源码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CentOS7入门_安装并配置mysql
- 下一篇: svn: Can't convert s