News for hackers. Stuff that matters.
Re:The world's smallest and fastest classical JavaScript inh... by jtblin on Saturday February 15, 2014 @07:25PM (#7246121)
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.

There are no comments on this comment yet.