javascript 如何克隆js对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3474697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:16:59  来源:igfitidea点击:

How to clone js object?

javascript

提问by Harold Sota

Possible Duplicate:
What is the most efficient way to clone a JavaScript object?

可能的重复:
克隆 JavaScript 对象的最有效方法是什么?

How to clone js object with out reference like these:

如何在没有引用的情况下克隆 js 对象,如下所示:

{ ID: _docEl,
  Index: next,
  DocName: _el
}

Any ideas?

有任何想法吗?

采纳答案by thomasrutter

You'll have to iterate over the object and make copies of all its properties.

您必须遍历对象并复制其所有属性。

And then if any of its properties are also objects, assuming you want to clone those too, you'll have to recurse into them.

然后如果它的任何属性也是对象,假设你也想克隆它们,你将不得不递归到它们。

There's various methods for doing this here: What is the most efficient way to clone a JavaScript object?

这里有多种方法可以执行此操作: 克隆 JavaScript 对象的最有效方法是什么?

回答by BoltClock

Here's how I'd do it, based on thomasrutter's suggestion(untested code):

根据thomasrutter 的建议(未经测试的代码),我是这样做的:

function cloneObj(obj) {
    var clone = {};

    for (var i in obj) {
        if (obj[i] && typeof obj[i] == 'object') {
            clone[i] = cloneObj(obj[i]);
        } else {
            clone[i] = obj[i];
        }
    }

    return clone;
}

回答by Mahmoodvcs

You can use jQuery.extend:

您可以使用 jQuery.extend:

// Shallow copy
var newObject = jQuery.extend({}, oldObject);

// Deep copy
var newObject = jQuery.extend(true, {}, oldObject);

The following post is so helpful:

以下帖子很有帮助:

What is the most efficient way to deep clone an object in JavaScript?

在 JavaScript 中深度克隆对象的最有效方法是什么?

回答by the-teacher

JavaScript JS object clone

JavaScript JS 对象克隆

Object._clone = function(obj) {
  var clone, property, value;
  if (!obj || typeof obj !== 'object') {
    return obj;
  }
  clone = typeof obj.pop === 'function' ? [] : {};
  clone.__proto__ = obj.__proto__;
  for (property in obj) {
    if (obj.hasOwnProperty(property)) {
      value = obj.property;
      if (value && typeof value === 'object') {
        clone[property] = Object._clone(value);
      } else {
        clone[property] = obj[property];
      }
    }
  }
  return clone;
};

CoffeeScript JS object clone

CoffeeScript JS 对象克隆

# Object clone
Object._clone = (obj) ->
  return obj if not obj or typeof(obj) isnt 'object'
  clone = if typeof(obj.pop) is 'function' then [] else {}

  # deprecated, but need for instanceof method
  clone.__proto__ = obj.__proto__

  for property of obj
    if obj.hasOwnProperty property
      # clone properties
      value = obj.property
      if value and typeof(value) is 'object'
        clone[property] = Object._clone(value)
      else
        clone[property] = obj[property]

  clone

Now you can try to do that

现在你可以尝试这样做

A = new TestKlass
B = Object._clone(A)
B instanceof TestKlass => true

回答by Floyd

function objToClone(obj){
  return (new Function("return " + obj))
}