在node.js中克隆对象的最佳方法是什么
例:我想避免以下情况:
var obj1 = {x: 5, y:5};
var obj2 = obj1;
obj2.x = 6;
console.log(obj1.x); // logs 6
对象很可能包含复杂类型作为属性,所以简单的for(var x in obj1)解决不了问题。我需要自己写一个递归克隆吗?还是有一些我没有看到的内置的东西?
在node.js中克隆对象的最佳方法是什么
例:我想避免以下情况:
var obj1 = {x: 5, y:5};
var obj2 = obj1;
obj2.x = 6;
console.log(obj1.x); // logs 6
对象很可能包含复杂类型作为属性,所以简单的for(var x in obj1)解决不了问题。我需要自己写一个递归克隆吗?还是有一些我没有看到的内置的东西?
你可以使用JQuery的extend函数:
var newClone= jQuery.extend({}, oldObject);
var deepClone = jQuery.extend(true, {}, oldObject);
还有一个Node.js插件:
https://github.com/shimondoodkin/nodejs-clone-extend
要做到没有JQuery或插件阅读这里:
http://my.opera.com/GreyWyvern/blog/show.dml/1725165
Object.defineProperty(Object.prototype, "extend", {
enumerable: false,
value: function(from) {
var props = Object.getOwnPropertyNames(from);
var dest = this;
props.forEach(function(name) {
if (name in dest) {
var destination = Object.getOwnPropertyDescriptor(from, name);
Object.defineProperty(dest, name, destination);
}
});
return this;
}
});
这将定义一个您可以使用的扩展方法。代码来自本文。
在NodeJS中克隆Object的最简单和最快的方法是使用Object。Keys (obj)方法
var a = {"a": "a11", "b": "avc"};
var b;
for(var keys = Object.keys(a), l = keys.length; l; --l)
{
b[ keys[l-1] ] = a[ keys[l-1] ];
}
b.a = 0;
console.log("a: " + JSON.stringify(a)); // LOG: a: {"a":"a11","b":"avc"}
console.log("b: " + JSON.stringify(b)); // LOG: b: {"a":0,"b":"avc"}
方法Object。keys需要JavaScript 1.8.5;nodeJS v0.4.11支持此方法
当然对于嵌套对象需要实现递归func
另一个解决方案是使用原生JSON(在JavaScript 1.7中实现),但它比之前的要慢得多(大约慢10倍)
var a = {"a": i, "b": i*i};
var b = JSON.parse(JSON.stringify(a));
b.a = 0;
如果你正在使用coffee-script,它就像:
newObject = {}
newObject[key] = value for own key,value of oldObject
虽然这不是一个深度克隆。
如果你不想“滚出你自己的”,还有一些Node模块。这个看起来不错:https://www.npmjs.com/package/clone
看起来它可以处理各种各样的东西,包括循环引用。来自github页面:
clone主人克隆对象,数组,日期对象和RegEx 对象。所有内容都是递归克隆的,因此您可以克隆日期 例如,在对象的数组中。[…循环推荐?是的!
在Github上也有一个项目,旨在成为jQuery.extend()的更直接的端口:
https://github.com/dreamerslab/node.extend
下面是一个例子,来自jQuery文档:
var extend = require('node.extend');
var object1 = {
apple: 0,
banana: {
weight: 52,
price: 100
},
cherry: 97
};
var object2 = {
banana: {
price: 200
},
durian: 100
};
var merged = extend(object1, object2);
在寻找一个真正的克隆选项时,我偶然发现了一个可笑的链接:
http://my.opera.com/GreyWyvern/blog/show.dml/1725165
我修改了该页上的解决方案,以便附加到Object原型的函数是不可枚举的。这是我的结果:
Object.defineProperty(Object.prototype, 'clone', {
enumerable: false,
value: function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
}
});
希望这也能帮助到其他人。注意,这里有一些注意事项……特别是对于名为“clone”的属性。这对我来说很有效。这不是我写的。同样,我只是改变了它的定义方式。
可能性1
低装饰深度复制:
var obj2 = JSON.parse(JSON.stringify(obj1));
可能性2(已弃用)
注意:这个解决方案现在在Node.js的文档中被标记为已弃用:
util._extend()方法从未打算在内部Node.js模块之外使用。社区发现并使用了它。 它已弃用,不应在新代码中使用。JavaScript通过Object.assign()提供了非常类似的内置功能。
原来的答案::
对于浅拷贝,使用Node内置的util._extend()函数。
var extend = require('util')._extend;
var obj1 = {x: 5, y:5};
var obj2 = extend({}, obj1);
obj2.x = 6;
console.log(obj1.x); // still logs 5
Node的_extend函数的源代码在这里:https://github.com/joyent/node/blob/master/lib/util.js
exports._extend = function(origin, add) {
// Don't do anything if add isn't an object
if (!add || typeof add !== 'object') return origin;
var keys = Object.keys(add);
var i = keys.length;
while (i--) {
origin[keys[i]] = add[keys[i]];
}
return origin;
};
没有一个答案让我满意,一些不工作或只是肤浅的克隆,答案来自@clint-harris和使用JSON。Parse /stringify很好,但是很慢。我找到了一个可以快速进行深度克隆的模块:https://github.com/AlexeyKupershtokh/node-v8-clone
你也可以在NodeJS中使用SugarJS。
http://sugarjs.com/
它们有一个非常干净的克隆特性: http://sugarjs.com/api/Object/clone
There is no built-in way to do a real clone (deep copy) of an object in node.js. There are some tricky edge cases so you should definitely use a library for this. I wrote such a function for my simpleoo library. You can use the deepCopy function without using anything else from the library (which is quite small) if you don't need it. This function supports cloning multiple data types, including arrays, dates, and regular expressions, it supports recursive references, and it also works with objects whose constructor functions have required parameters.
代码如下:
//If Object.create isn't already defined, we just do the simple shim, without the second argument,
//since that's all we need here
var object_create = Object.create;
if (typeof object_create !== 'function') {
object_create = function(o) {
function F() {}
F.prototype = o;
return new F();
};
}
/**
* Deep copy an object (make copies of all its object properties, sub-properties, etc.)
* An improved version of http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone
* that doesn't break if the constructor has required parameters
*
* It also borrows some code from http://stackoverflow.com/a/11621004/560114
*/
function deepCopy = function deepCopy(src, /* INTERNAL */ _visited) {
if(src == null || typeof(src) !== 'object'){
return src;
}
// Initialize the visited objects array if needed
// This is used to detect cyclic references
if (_visited == undefined){
_visited = [];
}
// Ensure src has not already been visited
else {
var i, len = _visited.length;
for (i = 0; i < len; i++) {
// If src was already visited, don't try to copy it, just return the reference
if (src === _visited[i]) {
return src;
}
}
}
// Add this object to the visited array
_visited.push(src);
//Honor native/custom clone methods
if(typeof src.clone == 'function'){
return src.clone(true);
}
//Special cases:
//Array
if (Object.prototype.toString.call(src) == '[object Array]') {
//[].slice(0) would soft clone
ret = src.slice();
var i = ret.length;
while (i--){
ret[i] = deepCopy(ret[i], _visited);
}
return ret;
}
//Date
if (src instanceof Date) {
return new Date(src.getTime());
}
//RegExp
if (src instanceof RegExp) {
return new RegExp(src);
}
//DOM Element
if (src.nodeType && typeof src.cloneNode == 'function') {
return src.cloneNode(true);
}
//If we've reached here, we have a regular object, array, or function
//make sure the returned object has the same prototype as the original
var proto = (Object.getPrototypeOf ? Object.getPrototypeOf(src): src.__proto__);
if (!proto) {
proto = src.constructor.prototype; //this line would probably only be reached by very old browsers
}
var ret = object_create(proto);
for(var key in src){
//Note: this does NOT preserve ES5 property attributes like 'writable', 'enumerable', etc.
//For an example of how this could be modified to do so, see the singleMixin() function
ret[key] = deepCopy(src[key], _visited);
}
return ret;
};
还有另一个库lodash,它有clone和cloneDeep。
Clone将克隆您的对象,但不会为非原语值创建一个新实例,而是使用对原始对象的引用
cloneDeep将在不引用原始对象的情况下创建新的对象,因此当您随后必须更改对象时更安全。
这段代码也是object .create()方法使用指定的原型对象和属性创建一个新对象的原因。
var obj1 = {x:5, y:5};
var obj2 = Object.create(obj1);
obj2.x; //5
obj2.x = 6;
obj2.x; //6
obj1.x; //5
我很惊讶,客体。还没有提到Assign。
let cloned = Object.assign({}, source);
如果可用(例如Babel),你可以使用对象展开操作符:
let cloned = { ... source };
你可以原型对象,然后调用对象实例,每次你想使用和改变对象:
function object () {
this.x = 5;
this.y = 5;
}
var obj1 = new object();
var obj2 = new object();
obj2.x = 6;
console.log(obj1.x); //logs 5
也可以将参数传递给对象构造函数
function object (x, y) {
this.x = x;
this.y = y;
}
var obj1 = new object(5, 5);
var obj2 = new object(6, 6);
console.log(obj1.x); //logs 5
console.log(obj2.x); //logs 6
希望这对你有帮助。
你也可以使用这个克隆库来深度克隆对象。
npm install --save clone
const clone = require('clone');
const clonedObject = clone(sourceObject);
如果你正在使用普通对象和数组,并且不关心克隆函数或递归引用,这里有一个简单的deepClone实现,它适用于普通对象、数组、字符串、数字、正则表达式、日期等。
// Simple Deep Clone
// Does not clone functions or handle recursive references.
function deepClone(original) {
if (original instanceof RegExp) {
return new RegExp(original);
} else if (original instanceof Date) {
return new Date(original.getTime());
} else if (Array.isArray(original)) {
return original.map(deepClone);
} else if (typeof original === 'object' && original !== null) {
const clone = {};
Object.keys(original).forEach(k => {
clone[k] = deepClone(original[k]);
});
return clone;
}
return original;
}
// Usage:
const obj = { n: 1, a: [ { a: 1 }, { a: 2 } ], d: new Date(), s: 'foo' };
const clone = deepClone(obj);
关于这个问题的好文章:https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/
var obj1 = {x: 5, y:5};
var obj2 = Object.assign({}, obj1 );
obj2 = {z: 10};
console.log(obj1);
console.log(obj2);