jquery源码--extend函数的归纳
来源:互联网 发布:2015中国国际储备数据 编辑:程序博客网 时间:2024/06/05 21:00
初入前端大门,边熟悉JS语法,边做项目,边看看jquery源码。对jQuery的功能都还不算熟悉,更没什么整体的概念,所以打算从一些点开始。打算就着W3C上的jquery教程,把每一项功能过一遍,然后再扒一下源码实现。extend函数实现对象复制,将一个或多个源对象中的属性复制到目标对象中。
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 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;};
1,判断拷贝深浅;
深浅不同,拷贝策略不同。
2,判断参数;
只有一个源对象时,目标对象设为jquery自身。
有些目标对象非法(如:number),则需要重设为一个新的Object。
3,遍历每个源对象的每个属性执行复制操作;
如果需要深拷贝&&(目标是Object || 目标是数组)则执行递归,递归复制每个属性,直至属性无法深拷贝(如:number和string就无法深拷贝)为止。
所以,深浅拷贝的区别也可以看出,即:引用和复制的区别,浅拷贝只是对目标对象的引用,而深拷贝则会创建对象并赋值。
阅读全文
0 0
- jquery源码--extend函数的归纳
- jQuery源码分析-extend函数
- jQuery.extend函数源码详解
- jquery源码--each函数归纳
- jquery源码分析之扩展函数 extend, $.extend
- jQuery源码——extend()函数
- jquery源码-jquery.extend()
- jQuery源码-jQuery.extend
- $.extend,jQuery.extend 函数详解
- jquery.extend源码分析
- jQuery.extend()源码解析
- Jquery的extend函数的简单应用
- jQuery.extend扩展函数的用法
- 神奇的js,jQuery.extend 函数详解
- jQuery.extend函数的多种用法!
- Jquery extend 函数的 用法 解析
- jQuery.extend 函数详解
- jQuery.extend 函数详解
- [NLP技术]关键词提取算法-TextRank
- 深度学习入门---只要300页ppt就够了!!!
- view的执行过程,各种方法的调用先后顺序
- OpenGL ES 2.0 VBO与渲染
- Python中的if-else
- jquery源码--extend函数的归纳
- Storm的配置文件yaml详解
- pat 乙级 1023. 组个最小数 (20)
- 利用Gate one和ngrok把内网中kali映射到公网,方便随时随地去使用kali进行操作
- Rand5() to Rand7()
- linux下查一个软件的后台软件及其调用参数
- HITOJ 2739 The Chinese Postman Problem(有向带权图上的中国邮递员问题)
- 产学研的含义
- Hdu 2062 Subset sequence