博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery源码 - extend 继承&拷贝 解析
阅读量:6983 次
发布时间:2019-06-27

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

实现原理

浅拷贝 jQuery.extend(clone, copy)

实现原理:

key值不同: 以clone、copy合集为准

key值相同: value值为基本类型直接覆盖; 为对象或数组若 则以copy为准;

#### 深拷贝 jQuery.extend(true, clone, copy)

实现原理:

key值不同: 以clone、copy合集为准

key值相同: value值为基本类型直接覆盖;
为对象或数组 嵌套形式则取两者合集, 直接对象或数组则直接覆盖;

### jQuery 源码片段

/* 继承实现 依赖 */// Object对象var class2type = {};var toString = class2type.toString;// 检测是否为对象本身属性而非继承var hasOwn = class2type.hasOwnProperty; // 获取对象原型函数var getProto = Object.getPrototypeOf;// 对象的实现(函数体)var fnToString = hasOwn.toString;// Object实现 "function Object() { [native code] }"var ObjectFunctionString = fnToString.call( Object );// 是否为纯对象或数组, 目的:是否需要深度遍历对象var isPlainObject = function( obj ) {    var proto, Ctor;    // obj 是否为对象    if ( !obj || toString.call( obj ) !== "[object Object]" ) {        return false;    }        // obj 对象原型    proto = getProto( obj );    // 是否有原型对象 Object.create( null )无[prototype]    if ( !proto ) {        return true;    }    //获取对象构造函数,若为Object直接创建则为 function Object()     Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;    //是否为Object直接对象    return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;};
/* 继承实现 */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;        // Skip the boolean and the target        target = arguments[ i ] || {};        i++;    }    // 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 ( i === length ) {        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 = Array.isArray( copy ) ) ) ) {                    if ( copyIsArray ) {                        copyIsArray = false;                        clone = src && Array.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;};

转载地址:http://kmtpl.baihongyu.com/

你可能感兴趣的文章
网址被微信拦截怎么办 微信屏蔽的域名如何正常访问
查看>>
@ModelAttribute运用详解
查看>>
思科交换机VTP配置
查看>>
正则表达式
查看>>
Mysql中使用命令行导入.sql文件新建数据库表(图文)
查看>>
RUBY有感
查看>>
spring 配置多数据源
查看>>
Java 线程数据交换控制器Exchange使用实例
查看>>
IBM X系列服务器IMM日志采集
查看>>
实验三 静态路由、默认路由配置
查看>>
mysql 查看导出数据字典
查看>>
linux命令--cp
查看>>
到底怎么样才叫看书?
查看>>
python 将ipv4的格式转换
查看>>
C语言宏的副作用的简单实例
查看>>
关于C语言结构体对齐的学习
查看>>
富文本框
查看>>
windows下安装rabbitMQ
查看>>
20个优秀的移动(iPhone)网站设计案例
查看>>
week04_python函数返回值、作用域
查看>>