This is nice but not quite right, it only works when overwriting the constructor. Every object in Javascript as a constructor property defined
var a = {}; a.constructor
// display function Object() { [native code] }
Therefore the test on `properties.constructor` doesn't work to extend objects without overwriting the constructor e.g.
var MyString = augment(String, {});
var myString = new MyString;
myString
// display Object {}
myString.length
// display undefined
To solve this you need to test if the constructor is not the default one i.e.
var obj = {};
var child = properties && properties.constructor && properties.constructor !== obj.constructor ? properties.constructor : function() {
return parent.apply(this, arguments);
};
Now you can extend objects without providing a new constructor:
var MyString = augment(String, {});
var myString = new MyString;
myString
// display child {constructor: function}
myString.length
// display 0
Nice little utility other than that.