From 60025bd048c82031bb0c9cf92ed4b63b5f828d51 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 1 Dec 2010 23:27:31 -0500 Subject: [PATCH] Added extend method to Interface --- lib/interface.js | 27 +++++++++++++++++++++++++++ test/test-interface-extend.js | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+) 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" +); +