jquery深度拷贝extend方法封装
来源:互联网 发布:德军总部 知乎 编辑:程序博客网 时间:2024/05/16 18:08
应用场景
最近在搞微信小程序,这里面直接引入jquery的话会报错,但是项目用到了jq的extend方法,在这里抽离出这个函数,可以引入直接应用
抽离源码
var class2type = {};var toString = class2type.toString;var hasOwn = class2type.hasOwnProperty;var support = {};function extend() { var src, copyIsArray, copy, name, options, 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" && !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 && ( isPlainObject(copy) || (copyIsArray = isArray(copy)) ) ) { if ( copyIsArray ) { copyIsArray = false; clone = src && isArray(src) ? src : []; } else { clone = src && isPlainObject(src) ? src : {}; } // Never move original objects, clone them target[ name ] = extend( deep, clone, copy ); // Don't bring in undefined values } else if ( copy !== undefined ) { target[ name ] = copy; } } } } // Return the modified object return target;}// isPlainObject isFunction isArray isWindow typefunction isPlainObject( obj ) { var key; // Must be an Object. // Because of IE, we also have to check the presence of the constructor property. // Make sure that DOM nodes and window objects don't pass through, as well if ( !obj || type(obj) !== "object" || obj.nodeType || isWindow( obj ) ) { return false; } try { // Not own constructor property must be Object if ( obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) { return false; } } catch ( e ) { // IE8,9 Will throw exceptions on certain host objects #9897 return false; } // Support: IE<9 // Handle iteration over inherited properties before own properties. if ( support.ownLast ) { for ( key in obj ) { return hasOwn.call( obj, key ); } } // Own properties are enumerated firstly, so to speed up, // if last one is own, then all properties are own. for ( key in obj ) {} return key === undefined || hasOwn.call( obj, key );}function isFunction( obj ) { return type(obj) === "function";}function isArray( obj ) { return type(obj) === "array";}function isWindow( obj ) { /* jshint eqeqeq: false */ return obj != null && obj == obj.window;}function type( obj ) { if ( obj == null ) { return obj + ""; } return typeof obj === "object" || typeof obj === "function" ? class2type[ toString.call(obj) ] || "object" : typeof obj;}
应用示例
// 浅拷贝var result1 = extend({}, { name: "John", location: {city: "Boston",county:"USA"} }, { last: "Resig", location: {state: "MA",county:"China"} } ); console.log(result1);// 深拷贝var result2 = extend(true, {}, { name: "John", location: {city: "Boston",county:"USA"} }, { last: "Resig", location: {state: "MA",county:"China"} } ); console.log(result2);
1 0
- jquery深度拷贝extend方法封装
- jquery extend深度拷贝
- 对象的拷贝 - $.extend方法
- jQuery中的extend方法
- Jquery的$.extend方法
- jquery extend 方法
- jQuery extend 扩展方法
- jQuery中的extend方法
- jQuery.extend()方法
- jQuery的extend方法
- jquery.extend() 方法详解
- jQuery.extend()方法
- Jquery的extend方法$.extend()学习
- JQuery.extend深度讲解,浅谈Object.create
- jQuery.extend()方法和jQuery.fn.extend()方法源码分析
- JQuery的extend扩展方法
- JQuery的extend扩展方法
- jquery的extend方法详解
- RxJava连接操作符
- JAVA+JDBC对数据库的数据进行增加、修改、删除、查询等操作
- (Hibernate)JPA注解总结
- 完美替代Dreamwave的webstorm
- 图像匹配-中北android实验室计算机视觉第二讲笔记
- jquery深度拷贝extend方法封装
- 2016-11-11 Redis
- WeatherView -- 自定义View实现天气折线图
- AOI中光板拍照点位的配置
- C++基础知识
- glxinfo
- 导入excel的时候如何选择其他sheet页
- 在Linux服务器上搭建一个论坛
- 生活随笔:不要轻易说放弃