ไม่มีวิธีในตัวที่จะทำการโคลนจริง (คัดลอกลึก) ของวัตถุใน node.js มีบางกรณีที่มีขอบหากินดังนั้นคุณควรใช้ห้องสมุดนี้ ฉันเขียนฟังก์ชั่นดังกล่าวสำหรับห้องสมุดsimpleooของฉัน คุณสามารถใช้deepCopyฟังก์ชั่นโดยไม่ต้องใช้สิ่งอื่นจากไลบรารี (ซึ่งค่อนข้างเล็ก) หากคุณไม่ต้องการ ฟังก์ชันนี้รองรับการโคลนหลายประเภทข้อมูลรวมถึงอาร์เรย์วันที่และนิพจน์ทั่วไปรองรับการอ้างอิงแบบเรียกซ้ำและยังทำงานกับวัตถุที่ฟังก์ชันตัวสร้างมีพารามิเตอร์ที่ต้องการ
นี่คือรหัส:
//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;
};
npm install underscore2.var _ = require('underscore')3._.clone(objToClone);