博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery扩展
阅读量:7142 次
发布时间:2019-06-28

本文共 1899 字,大约阅读时间需要 6 分钟。

jQuery有一个方法特别有用,extend。jQuery自身通过该方法对jQuery进行扩展,在api中用户也可以根据需要对jQuery扩展。

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	if ( typeof target === "boolean" ) {		deep = target;		target = arguments[1] || {};		// skip the boolean and the target		i = 2;	}	// Handle case when target is a string or something (possible in deep copy)	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 object			for ( 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)) ) ) {					if ( copyIsArray ) {						copyIsArray = false;						clone = src && jQuery.isArray(src) ? src : [];					} else {						clone = src && jQuery.isPlainObject(src) ? src : {};					}					// Never move original objects, clone them					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;  //返回应该算多余,你传递的绑定对象已经modify了。直接使用就好。不改变绑定对象的话还行。};

  如果需要对jquery类型扩展需要对$.fn原型扩展。

$.extend({	test: function(){		console.log("扩展jQuery");		}});$.extend($.fn,{	test: function(){		console.log("扩展jQuery原型");		}});$.test();$("*").test();

  

转载于:https://www.cnblogs.com/simpman/archive/2013/05/11/3072754.html

你可能感兴趣的文章
项目需求讨论-APP手势解锁及指纹解锁
查看>>
CocoaPods 给每个库单独指定 Swift 版本教程
查看>>
今年第一个独立 App,TKeyboard,也是第一个开源项目
查看>>
Mongodb数据库误删后的恢复
查看>>
整理些PHP的学习方向资料
查看>>
关于vue开发的常见问题
查看>>
IT,互联网,科技,技术博客网站推荐
查看>>
如何实现全屏遮罩(附Vue.extend和el-message源码学习)
查看>>
你或许不知道Vue的这些小技巧
查看>>
Promise源码学习(1)
查看>>
[项目推荐] Corcel 让你在 WordPress 中使用 Laravel
查看>>
阿里:千亿交易背后的0故障发布
查看>>
Node+express+mongoose 基础笔记
查看>>
利用angular4和nodejs-express构建一个简单的网站(十)—好友模块
查看>>
极光大数据告诉你,程序员们都在"愁"些啥?
查看>>
python写一个简单的图形化记事本
查看>>
从Hash到散列表到HashMap
查看>>
前端基础知识学习记录(三)
查看>>
如何制作一个类似jquery插件的vue插件
查看>>
原型链类原理
查看>>