If you want to create a new object with the new operator, this can be tricky if you can no longer modify the constructor. As far as I know, the constructor is the only place where instance variables can be defined if you use the new operator.
If you were to create objects with Object.create , you can pass additional properties in the second parameter , which is similar to Object.defineProperty :
var User = function() { this.name = ''; } User.prototype.xyz = '';
If you need to do this before creating the object, you can always wrap your original constructor in another function:
var User = function() { this.name = ''; } User.prototype.xyz = '';
I personally believe that the version of Object.create is understandable and less error prone (especially if you have several properties that you want to add at different times).
source share