按值而不是按引用复制对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按值而不是按引用复制对象相关的知识,希望对你有一定的参考价值。

To do this for any object in javascript will not be simple or straightforward. You will run into the problem of erroneously picking up attributes from the object's prototype that should be left in the prototype and not copied to the new instance. If, for instance, you are adding a clone method to Object.prototype, as some answers depict, you will need to explicitly skip that attribute. But what if there are other additional methods added to Object.prototype, or other intermediate prototypes, that you don't know about? In that case, you will copy attributes you shouldn't, so you need to detect unforeseen, non-local attributes with the hasOwnProperty method.

In addition to non-enumerable attributes, you'll encounter a tougher problem when you try to copy objects that have hidden properties. For example, prototype is a hidden property of a function. Also, an object's prototype is referenced with the attribute __proto__, which is also hidden, and will not be copied by a for/in loop iterating over the source object's attributes. I think __proto__ might be specific to Firefox's JavaScript interpreter and it may be something different in other browsers, but you get the picture. Not everything is enumerable. You can copy a hidden attribute if you know its name, but I don't know of any way to discover it automatically.

Yet another snag in the quest for an elegant solution is the problem of setting up the prototype inheritance correctly. If your source object's prototype is Object, then simply creating a new general object with {} will work, but if the source's prototype is some descendant of Object, then you are going to be missing the additional members from that prototype which you skipped using the hasOwnProperty filter, or which were in the prototype, but weren't enumerable in the first place. One solution might be to call the source object's constructor property to get the initial copy object and then copy over the attributes, but then you still will not get non-enumerable attributes. For example, a Date object stores its data as a hidden member:

The date string for d1 will be 5 seconds behind that of d2. A way to make one Date the same as another is by calling the setTime method, but that is specific to the Date class. I don't think there is a bullet-proof general solution to this problem, though I would be happy to be wrong!

When I had to implement general deep copying I ended up compromising by assuming that I would only need to copy a plain Object, Array, Date, String, Number, or Boolean. The last 3 types are immutable, so I could perform a shallow copy and not worry about it changing. I further assumed that any elements contained in Object or Array would also be one of the 6 simple types in that list. This can be accomplished with code like the following:

The above function will work adequately for the 6 simple types I mentioned, as long as the data in the objects and arrays form a tree structure. That is, there isn't more than one reference to the same data in the object. For example:
  1. function clone(obj) {
  2. var copy;
  3.  
  4. // Handle the 3 simple types, and null or undefined
  5. if (null == obj || "object" != typeof obj) return obj;
  6.  
  7. // Handle Date
  8. if (obj instanceof Date) {
  9. copy = new Date();
  10. copy.setTime(obj.getTime());
  11. return copy;
  12. }
  13.  
  14. // Handle Array
  15. if (obj instanceof Array) {
  16. copy = [];
  17. for (var i = 0, len = obj.length; i < len; i++) {
  18. copy[i] = clone(obj[i]);
  19. }
  20. return copy;
  21. }
  22.  
  23. // Handle Object
  24. if (obj instanceof Object) {
  25. copy = {};
  26. for (var attr in obj) {
  27. if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
  28. }
  29. return copy;
  30. }
  31.  
  32. throw new Error("Unable to copy obj! Its type isn't supported.");
  33. }

以上是关于按值而不是按引用复制对象的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 2 中,无论列表的内容如何,​​如何按值而不是引用复制复杂嵌套元素的列表

如何按值而不是按引用将数组添加到列表中?

Java 对象复制的最佳选择? [复制]

java中参数传递--值传递,引用传递

php按值复制数组元素,而不是按引用

试图取消引用一个接口,该接口是一个指向后端结构对象的指针,以便我可以按值传递给函数