Ray*_*nos 27
它已被移植.节点延伸
请注意,该项目没有测试,也没有太大的受欢迎程度,因此使用风险由您自行承担.
如上所述,您可能不需要深层复制.尝试更改您的数据结构,这样您只需要浅拷贝.
我写了一个较小的模块,建议你使用xtend.它没有包含jQuery行李的实现,也没有像node-extend那样的bug.
jco*_*and 15
你想要jQuery,所以只需使用它:
function extend() {
var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false,
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty,
push = Array.prototype.push,
slice = Array.prototype.slice,
trim = String.prototype.trim,
indexOf = Array.prototype.indexOf,
class2type = {
"[object Boolean]": "boolean",
"[object Number]": "number",
"[object String]": "string",
"[object Function]": "function",
"[object Array]": "array",
"[object Date]": "date",
"[object RegExp]": "regexp",
"[object Object]": "object"
},
jQuery = {
isFunction: function (obj) {
return jQuery.type(obj) === "function"
},
isArray: Array.isArray ||
function (obj) {
return jQuery.type(obj) === "array"
},
isWindow: function (obj) {
return obj != null && obj == obj.window
},
isNumeric: function (obj) {
return !isNaN(parseFloat(obj)) && isFinite(obj)
},
type: function (obj) {
return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"
},
isPlainObject: function (obj) {
if (!obj || jQuery.type(obj) !== "object" || obj.nodeType) {
return false
}
try {
if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
return false
}
} catch (e) {
return false
}
var key;
for (key in obj) {}
return key === undefined || hasOwn.call(obj, key)
}
};
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
i = 2;
}
if (typeof target !== "object" && !jQuery.isFunction(target)) {
target = {}
}
if (length === i) {
target = this;
--i;
}
for (i; i < length; i++) {
if ((options = arguments[i]) != null) {
for (name in options) {
src = target[name];
copy = options[name];
if (target === copy) {
continue
}
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 : {};
}
// WARNING: RECURSION
target[name] = extend(deep, clone, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
}
Run Code Online (Sandbox Code Playgroud)
和一个小测试,以表明它做了深层复制
extend(true,
{
"name": "value"
},
{
"object": "value",
"other": "thing",
"inception": {
"deeper": "deeper",
"inception": {
"deeper": "deeper",
"inception": {
"deeper": "deeper"
}
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
但请记住提供归因:https://github.com/jquery/jquery/blob/master/src/core.js
Kat*_*ato 11
深度拷贝的快速而肮脏的答案只是用一点点JSON作弊.它不是最高效的,但它确实做得非常好.
function clone(a) {
return JSON.parse(JSON.stringify(a));
}
Run Code Online (Sandbox Code Playgroud)
小智 11
请使用内置的util模块:
var extend = require('util')._extend;
var merged = extend(obj1, obj2);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40503 次 |
| 最近记录: |