diff --git a/lib/interface.js b/lib/interface.js index f164fff..9ae38d2 100644 --- a/lib/interface.js +++ b/lib/interface.js @@ -58,6 +58,7 @@ function extend() util.propCopy( props, prototype ); + attach_extend( new_interface ); new_interface.prototype = prototype; new_interface.constructor = new_interface; @@ -90,3 +91,29 @@ function prop_check( props ) return props; } + +/** + * Attaches extend method to the given function (interface) + * + * @param {Function} func function (interface) to attach method to + * + * @return {undefined} + */ +function attach_extend( func ) +{ + /** + * Shorthand for extending interfaces + * + * This method can be invoked on the object, rather than having to call + * Interface.extend( this ). + * + * @param {Object} props properties to add to extended interface + * + * @return {Object} extended interface + */ + util.defineSecureProp( func, 'extend', function( props ) + { + return extend( this, props ); + }); +} + diff --git a/test/test-interface-extend.js b/test/test-interface-extend.js index 023d465..c08f2fc 100644 --- a/test/test-interface-extend.js +++ b/test/test-interface-extend.js @@ -91,3 +91,26 @@ assert.ok( "Interfaces can be extended with additional abstract methods" ); + +assert.ok( + ( BaseType.extend instanceof Function ), + "Interface contains extend method" +); + + +var SubType2 = BaseType.extend( +{ + second: abstractMethod(), +}); + +assert.ok( + ( new SubType2 instanceof BaseType ), + "Interface extend method can extend interfaces" +); + +assert.ok( + ( SubType2.prototype.second instanceof Function ), + "Interfaces can be extended with additional abstract methods using " + + "shorthand extend method" +); +