在javascript中复制对象

在javascript中复制对象,javascript,copy,Javascript,Copy,我想将对象从xwalk.utils.converter复制到xwalk.converter 转换器具有以下功能。 它是一个可用的对象副本吗?否则别名 xwalk.utils.converter.toArray xwalk.utils.converter.toBoolean xwalk.utils.converter.toByte xwalk.utils.converter.toDictionary xwalk.utils.converter.toDouble xwalk.utils.conver

我想将对象从xwalk.utils.converter复制到xwalk.converter 转换器具有以下功能。 它是一个可用的对象副本吗?否则别名

xwalk.utils.converter.toArray

xwalk.utils.converter.toBoolean

xwalk.utils.converter.toByte

xwalk.utils.converter.toDictionary

xwalk.utils.converter.toDouble

xwalk.utils.converter.toEnum

xwalk.utils.converter.toFunction

xwalk.utils.converter.toLong

xwalk.utils.converter.toLongLong

xwalk.utils.converter.toOctet

xwalk.utils.converter.toPlatformObject

xwalk.utils.converter.toShort

xwalk.utils.converter.toUnsignedLong

xwalk.utils.converter.toUnsignedLongLong

xwalk.utils.converter.toUnsignedShort


未经测试的代码,递归复制元素:

function copy(src) {
    var destination = {};
    for (var index in src) {
        if (typeof src[index] === "object") {
            destination[index] = copy(src[index]);
        } else if (Array.isArray(src[index])) {
            destination[index] = [];
            for (var arrIndex in src[index]) {
                if ((typeof src[index][arrIndex] === "object") || (Array.isArray(src[index][arrIndex]))) {
                    destination[index][arrIndex] = copy(src[index][arrIndex]);
                } else {
                    destination[index][arrIndex] = src[index];
                }
            }
        } else {
            destination[index] = src[index];
        }
    }
    return destination;
}
用法:

xwalk.converter = xwalk.utils.converter;
使用此功能:

var copy = function(from, to) {
  for (var prop in from) {
    if (from.hasOwnProperty(prop)) {
      to[prop] = from[prop];
    }
  }
};

copy(xwalk.utils.converter,xwalk.converter);

您可以使用深度扩展/复制,请参阅thnx,但它不起作用。。。因为xwalk是这样加载的。。var xwalk_loader=require'node-xwalk';xwalk_loader.extension_path.push'/usr/lib/extensions crosswalk/';var xwalk=xwalk_加载器。需要'utils';它不是js对象?