javascript - Copy object with results of getters -


i have object contains getter.

myobject {     id: "myid",     title () { return myrepository.title; } }  myrepository.title = "mytitle"; 

i want obtain object like:

myresult = {     id: "myid",      title: "mytitle" } 

i don't want copy getter, so:

myresult.title;                       // returns "mytitle" myrepository.title = "another title"; myresult.title;                       // should still return "mytitle" 

i've try:

  • $.extend(): doesn't iterate on getters. http://bugs.jquery.com/ticket/6145
  • iterating properties suggested here, doesn't iterate on getters.
  • as i'm using angular, using angular.foreach, suggested here. properties , not getters.

any idea? thx!

update setting getter using object.defineproperty as:

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

as can read in doc:

enumerable true if , if property shows during enumeration of properties on corresponding object. defaults false.

setting enumerable: true fix problem.

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

$.extend want. (update: you've since said want non-enumerable properties well, doesn't want; see second part of answer below, i'll leave first bit others.) bug isn't saying resulting object won't have title property, it's saying resulting object's title property won't getter, perfect said wanted.

example correct getter syntax:

// myrepository object  var myrepository = { title: "mytitle" };    // object getter  var myobject = {      id: "myid",      title() { return myrepository.title; }  };    // copy plain property  var copy = $.extend({}, myobject);    // view copy (although actually, result  // same either way)  snippet.log(json.stringify(copy));    // prove copy's `title` 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 `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, =, , ;

  • removed duplicate property title

  • corrected getter declaration syntax


if want include non-enumerable properties, you'll need use object.getownpropertynames because won't show in for-in loop, object.keys, or $.extend (whether or not they're "getter" or normal properties):

// myrepository object  var myrepository = { title: "mytitle" };    // object getter  var myobject = {      id: "myid"  };  object.defineproperty(myobject, "title", {    enumerable: false, // it's default, emphasis,    get: function() {      return myrepository.title;    }  });    snippet.log("$.extend won't visit non-enumerable properties, id here:");  snippet.log(json.stringify($.extend({}, myobject)));    // copy  var copy = object.getownpropertynames(myobject).reduce(function(result, name) {    result[name] = myobject[name];    return result;  }, {});    // view copy (although actually, result  // same either way)  snippet.log("our copy operation object.getownpropertynames does, though:");  snippet.log(json.stringify(copy));    // prove copy's `title` 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 `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->  <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -