在JavaScript中克隆不可枚举的属性 [英] Cloning Non enumerable properties in javascript

查看:59
本文介绍了在JavaScript中克隆不可枚举的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对象中具有所有不可枚举的属性,我想克隆该对象.

I have all non - enumerable properties in object, I want to clone that object.

我的问题无法枚举的属性没有被克隆.

My problem non-enumerable properties are not getting cloned.

以下面的示例为例

 Object.defineProperty(this, 'prop', {
        get: function () {
            return prop;
        },
        set: function (value) {
            prop= value;
        }
    });

   Object.defineProperty(this, 'newprop', {
        get: function () {
            return newprop;
        },
        set: function (value) {
            newprop= value;
        }
    });

例如,在我的对象中,使用以下方法进行克隆时,我具有上述两个属性,但我的属性并未被克隆,我相信这是因为它们不可枚举.

For example I have above two properties in my object doing clone using following methods, my properties are not getting cloned I believe it is because they are non - enumerable.

   var newObject = $.extend({},oldObject);
   var newObject= Object.assign({},oldobject);

如何在javascript中复制不可枚举的属性.

How do I copy non-enumerable properties in javascript.

推荐答案

如果一个或多个属性不是不可枚举的,您如何自动神奇地枚举它们?

If one or more properties aren't enumerable, how do you want to auto-magically enumerate them?

既然您知道他们的名字,就应该这样做:

Since you know their names, you should do something like this:

var sourceObj = this;
var targetObj = {};

["prop", "otherProperty"].forEach(function(property) {
    targetObj[property] = sourceObj[property]; 
});

或者,无论何时定义不可枚举的属性,都可以构建整个属性名称数组:

Or you can build the whole property name array whenever you define a non-enumerable property:

   var propertyNames = [];

   Object.defineProperty(this, 'newprop', {
        get: function () {
            return newprop;
        },
        set: function (value) {
            newprop= value;
        }
    });
    propertyNames.push("newprop"); // <---


   Object.defineProperty(this, 'newprop2', {
        get: function () {
            return newprop;
        },
        set: function (value) {
            newprop= value;
        }
    });
    propertyNames.push("newprop2"); // <---

propertyNames.forEach(function(property) {
    targetObj[property] = sourceObj[property]; 
});

替代方法: Object.getOwnPropertyNames

Object.getOwnPropertyNames()方法返回所有数组直接在给定对象上找到的属性(可枚举与否).

The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

也许这是最好的方法. Object.getOwnPropertyNames 获取自己对象属性的名称(无论它们是否可枚举).也就是说,您可以避免构建整个 propertyNames 数组,并且这种方法应该适合您,因为您说所有属性都不可枚举:

Maybe this is the best approach. Object.getOwnPropertyNames gets the name of own object's properties either if they're enumerable or not. That is, you can avoid building the whole propertyNames array, and this approach should be fine with you because you said that all properties aren't enumerable:

var sourceObj = this;
var targetObj = {};    

Object.getOwnPropertyNames(sourceObj).forEach(function(property) {
    targetObj[property] = sourceObj[property]; 
});

这篇关于在JavaScript中克隆不可枚举的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆