Copy object with results of getters

南楼画角 提交于 2019-12-11 03:15:45

问题


I have an object that contains a getter.

myObject {
    id: "MyId",
    get title () { return myRepository.title; }
}

myRepository.title = "MyTitle";

I want to obtain an object like:

myResult = {
    id: "MyId", 
    title: "MyTitle"
}

I don't want to copy the getter, so:

myResult.title;                       // Returns "MyTitle"
myRepository.title = "Another title";
myResult.title;                       // Should still return "MyTitle"

I've try:

  • $.extend(): But it doesn't iterate over getters. http://bugs.jquery.com/ticket/6145
  • Iterating properties as suggested here, but it doesn't iterate over getters.
  • As I'm using angular, using Angular.forEach, as suggested here. But I only get properties and not getters.

Any idea? Thx!

Update I was setting the getter using Object.defineProperty as:

 "title": { get: function () { return myRepository.title; }},

As can be read in the doc:

enumerable true if and only if this property shows up during enumeration of the properties on the corresponding object. Defaults to false.

Setting enumerable: true fix the problem.

"title": { get: function () { return myRepository.title; }, enumerable: true },

回答1:


$.extend does exactly what you want. (Update: You've since said you want non-enumerable properties as well, so it doesn't do what you want; see the second part of this answer below, but I'll leave the first bit for others.) The bug isn't saying that the resulting object won't have a title property, it's saying that the resulting object's title property won't be a getter, which is perfect for what you said you wanted.

Example with correct getter syntax:

// The myRepository object
var myRepository = { title: "MyTitle" };

// The object with a getter
var myObject = {
    id: "MyId",
    get title() { return myRepository.title; }
};

// The copy with a plain property
var copy = $.extend({}, myObject);

// View the copy (although actually, the result would look
// the same either way)
snippet.log(JSON.stringify(copy));

// Prove that the copy's `title` really is just a plain property:
snippet.log("Before: copy.title = " + copy.title);
copy.title = "foo";
snippet.log("After:  copy.title = " + copy.title);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Syntax fixes:

  • Added missing var, =, and ;

  • Removed duplicate property title

  • Corrected the getter declaration syntax


If you want to include non-enumerable properties, you'll need to use Object.getOwnPropertyNames because they won't show up in a for-in loop, Object.keys, or $.extend (whether or not they're "getter" or normal properties):

// The myRepository object
var myRepository = { title: "MyTitle" };

// The object with a getter
var myObject = {
    id: "MyId"
};
Object.defineProperty(myObject, "title", {
  enumerable: false, // it's the default, this is just for emphasis,
  get: function() {
    return myRepository.title;
  }
});

snippet.log("$.extend won't visit non-enumerable properties, so we only get id here:");
snippet.log(JSON.stringify($.extend({}, myObject)));

// Copy it
var copy = Object.getOwnPropertyNames(myObject).reduce(function(result, name) {
  result[name] = myObject[name];
  return result;
}, {});

// View the copy (although actually, the result would look
// the same either way)
snippet.log("Our copy operation with Object.getOwnPropertyNames does, though:");
snippet.log(JSON.stringify(copy));

// Prove that the copy's `title` really is just a plain property:
snippet.log("Before: copy.title = " + copy.title);
copy.title = "foo";
snippet.log("After:  copy.title = " + copy.title);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>



回答2:


First of all, fix your syntax, though it probably is good in your actual code:

myObject = {
    id: "MyId",
    get title () { return myRepository.title; }
}

Now, to the answer. :)

You can just use a for..in loop to get all the properties, then save them as-is:

var newObj = {};
for (var i in myObject) {
    newObj[i] = myObject[i];
}

No jQuery, Angular, any other plugins needed!



来源:https://stackoverflow.com/questions/26863564/copy-object-with-results-of-getters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!