From ec9f24a926323a6699d7e4399dbb4fb36d86ae7f Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 10 Jan 2011 19:00:26 -0500 Subject: [PATCH] Moved implement() into its own method, accepting destination object as the first parameter --- lib/class.js | 61 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/class.js b/lib/class.js index 038e3a1..38781b2 100644 --- a/lib/class.js +++ b/lib/class.js @@ -57,27 +57,12 @@ exports.extend = function( base ) */ exports.implement = function() { - var len = arguments.length, - dest = {}, - arg = null, + var args = Array.prototype.slice.call( arguments ); - abstract_list = [], - implemented = []; + // apply to an empty (new) object + args.unshift( {} ); - // add each of the interfaces - for ( var i = 0; i < len; i++ ) - { - arg = arguments[ i ]; - - // copy all interface methods to the class (does not yet deep copy) - util.propCopy( arg.prototype, dest ); - implemented.push( arg ); - } - - var class_new = exports.extend( dest ); - getMeta( class_new.__cid ).implemented = implemented; - - return class_new; + return implement.apply( this, args ); }; @@ -359,6 +344,44 @@ var extend = ( function( extending ) } )( false ); +/** + * Implements interface(s) into an object + * + * This will copy all of the abstract methods from the interface into the + * destination object. + * + * @param {Object} dest destination object + * @param {...Interface} interfaces interfaces to implement into dest + * + * @return {Object} destination object with interfaces implemented + */ +var implement = function() +{ + var args = Array.prototype.slice.call( arguments ), + dest = args.shift(), + len = args.length, + arg = null, + + abstract_list = [], + implemented = []; + + // add each of the interfaces + for ( var i = 0; i < len; i++ ) + { + arg = args[ i ]; + + // copy all interface methods to the class (does not yet deep copy) + util.propCopy( arg.prototype, dest ); + implemented.push( arg ); + } + + var class_new = exports.extend( dest ); + getMeta( class_new.__cid ).implemented = implemented; + + return class_new; +} + + /** * Sets up common properties for the provided function (class) *