我有一个包含 setter/getter 的对象。

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

myRepository.title = "MyTitle";

我想获得一个像这样的对象:
myResult = {
    id: "MyId",
    title: "MyTitle"
}

我不想复制 setter/getter ,所以:
myResult.title;                       // Returns "MyTitle"
myRepository.title = "Another title";
myResult.title;                       // Should still return "MyTitle"

我试过了:
  • $ .extend():但是它不会遍历getter。 http://bugs.jquery.com/ticket/6145
  • 像建议的here一样迭代属性,但不会迭代getter。
  • 当我使用angular时,使用Angular.forEach,如here所示。但是我只会得到属性,而不会得到 setter/getter 。

  • 任何的想法?谢谢!

    更新
    我使用Object.defineProperty将 setter/getter 设置为:
     "title": { get: function () { return myRepository.title; }},
    

    可以在文档中阅读:



    设置可枚举:真正解决问题。
    "title": { get: function () { return myRepository.title; }, enumerable: true },
    

    最佳答案

    $.extend正是您想要的。 (更新:从那以后,您就说过您还想要不可枚举的属性,因此它不能满足您的要求;请参阅下面的答案的第二部分,但我将第一部分留给其他人。)该错误并不是说生成的对象没有title属性,而是说生成的对象的title属性不会是 setter/getter ,这对于您想要的内容来说是完美的。

    正确的getter语法示例:

    // 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>


    语法修复:
  • 添加了缺少的var=;
  • 删除重复属性title
  • 更正了 setter/getter 声明语法


  • 如果要包括不可枚举的属性,则需要使用Object.getOwnPropertyNames,因为它们不会出现在for-in循环,Object.keys$.extend中(无论它们是“getter”还是常规属性):

    // 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>

    关于javascript - 复制带有 setter/getter 结果的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26863564/

    10-16 21:50