[英]What is the best way to copy objects in Angular 2?
Angular 1.x has methods on the global angular object like angular.copy, angular.shallowCopy (not to mention others like angular.forEach) that don't seem to have an equivalent version in Angular 2. Perhaps it is just not documented. Angular 1.x在全局角度对象上有方法,如angular.copy,angular.shallowCopy(更不用说像angular.forEach这样的其他),它们似乎没有Angular 2中的等效版本。也许它只是没有记录。 If Angular 2 doesn't intend on providing these utils, what is the best way to get that functionality?
如果Angular 2不打算提供这些工具,那么获得该功能的最佳方法是什么?
I know I could roll my own shallow copy method using the logic from angular 1.x: 我知道我可以使用angular 1.x中的逻辑来滚动我自己的浅拷贝方法:
function shallowCopy(src, dst) {
if (isArray(src)) {
dst = dst || [];
for (var i = 0, ii = src.length; i < ii; i++) {
dst[i] = src[i];
}
} else if (isObject(src)) {
dst = dst || {};
for (var key in src) {
if (!(key.charAt(0) === '$' && key.charAt(1) === '$')) {
dst[key] = src[key];
}
}
}
return dst || src;
}
But the deep copy logic is much more complicated (obviously) 但深度复制逻辑要复杂得多(显然)
When the object is an array , I use: 当对象是数组时 ,我使用:
copyOfArray = originalArray.slice(0, originalArray.length);
Where originalArray
is the original array object. 其中
originalArray
是原始数组对象。
The built-in javascript array.slice(start, end)
function returns a subset of the array as a new array object ( JavaScript Array slice() Method ). 内置的javascript
array.slice(start, end)
函数返回数组的子集作为新的数组对象( JavaScript Array slice()Method )。 Setting the start and end boundaries to the size of the original array returns a copy of the whole array. 将开始和结束边界设置为原始数组的大小将返回整个数组的副本。
Why not simply duplicate the angular 1 copy function ? 为什么不简单地复制角度1复制功能?
From the sources ( need to adapt some stuff like the calls to error handlers, but basically : ) 从来源(需要调整一些东西,如调用错误处理程序,但基本上:)
function copy(source, destination, stackSource, stackDest) {
if (isWindow(source) || isScope(source)) {
throw ngMinErr('cpws',
"Can't copy! Making copies of Window or Scope instances is not supported.");
}
if (isTypedArray(destination)) {
throw ngMinErr('cpta',
"Can't copy! TypedArray destination cannot be mutated.");
}
if (!destination) {
destination = source;
if (isObject(source)) {
var index;
if (stackSource && (index = stackSource.indexOf(source)) !== -1) {
return stackDest[index];
}
// TypedArray, Date and RegExp have specific copy functionality and must be
// pushed onto the stack before returning.
// Array and other objects create the base object and recurse to copy child
// objects. The array/object will be pushed onto the stack when recursed.
if (isArray(source)) {
return copy(source, [], stackSource, stackDest);
} else if (isTypedArray(source)) {
destination = new source.constructor(source);
} else if (isDate(source)) {
destination = new Date(source.getTime());
} else if (isRegExp(source)) {
destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]);
destination.lastIndex = source.lastIndex;
} else if (isFunction(source.cloneNode)) {
destination = source.cloneNode(true);
} else {
var emptyObject = Object.create(getPrototypeOf(source));
return copy(source, emptyObject, stackSource, stackDest);
}
if (stackDest) {
stackSource.push(source);
stackDest.push(destination);
}
}
} else {
if (source === destination) throw ngMinErr('cpi',
"Can't copy! Source and destination are identical.");
stackSource = stackSource || [];
stackDest = stackDest || [];
if (isObject(source)) {
stackSource.push(source);
stackDest.push(destination);
}
var result, key;
if (isArray(source)) {
destination.length = 0;
for (var i = 0; i < source.length; i++) {
destination.push(copy(source[i], null, stackSource, stackDest));
}
} else {
var h = destination.$$hashKey;
if (isArray(destination)) {
destination.length = 0;
} else {
forEach(destination, function(value, key) {
delete destination[key];
});
}
if (isBlankObject(source)) {
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
for (key in source) {
destination[key] = copy(source[key], null, stackSource, stackDest);
}
} else if (source && typeof source.hasOwnProperty === 'function') {
// Slow path, which must rely on hasOwnProperty
for (key in source) {
if (source.hasOwnProperty(key)) {
destination[key] = copy(source[key], null, stackSource, stackDest);
}
}
} else {
// Slowest path --- hasOwnProperty can't be called as a method
for (key in source) {
if (hasOwnProperty.call(source, key)) {
destination[key] = copy(source[key], null, stackSource, stackDest);
}
}
}
setHashKey(destination,h);
}
}
return destination;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.