How to clone a prototype with property methods?


I am using the Typed.React library which includes a method to extend one prototype definition with that of another:

function extractPrototype(clazz) { var proto = {}; for (var key in clazz.prototype) { proto[key] = clazz.prototype[key]; } return proto; }

If the provided class defines property methods, this function has a side effect of executing the get method e.g.

var TestObject = (function () { function TestObject() { this.str = "test string"; } Object.defineProperty(TestObject.prototype, "TestProperty", { get: function () { console.log("exec get"); return this.str; }, set: function (value) { console.log("exec set"); this.str = value; }, enumerable: true, configurable: true }); return TestObject; })(); var extracted = extractPrototype(TestObject);

When extactPrototype accesses TestObject.prototype["TestProperty"], it will execute the property get method and print:

exec get

How would I duplicate a prototype with property methods without executing them?


I think you are looking for the new ES6 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign" rel="nofollow">Object.assign</a> function.

Of course there's a simpler fix to your problem - just don't access and set properties, copy their <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor" rel="nofollow">property descriptors</a>:

function extractPrototype(clazz) { var proto = {}; for (var key in clazz.prototype) { Object.defineProperty(proto, key, Object.getOwnPropertyDescriptor(clazz.prototype, key)); } return proto; }


  • Definition of server-class machine changed recently?
  • Will cin recognize \\n typed in from keyboard as a newline character?
  • How to avoid using dynamic variables / a billion if statements in Java?
  • Updating Dojo provide
  • Ember.js model to be organised as a tree structure
  • Parse a date string in a specific locale (not timezone!)
  • Jackson Parser: ignore deserializing for type mismatch
  • Database structure design with variable amounts of fields
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Why value captured by reference in lambda is broken? [duplicate]
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • How do I fake an specific browser client when using Java's Net library?
  • swift auto completion not working in Xcode6-Beta
  • How to get a value (ex: baseURL) in every Karate feature?
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • How to handle AllServersUnavailable Exception
  • JSON with duplicate key names losing information when parsed
  • How can I estimate amount of memory left with calling System.gc()?
  • Jquery - Jquery Wysiwyg return html as a string
  • 0x202A in filename: Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • C# - Getting references of reference
  • Why joiner is not used after Sequence generator or Update statergy
  • Java static initializers and reflection
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Recursive/Hierarchical Query Using Postgres
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases
  • How to push additional view controllers onto NavigationController but keep the TabBar?