@@ -241,28 +241,57 @@ const capMe = (arr) => {
241241 *
242242 * */
243243
244- //let junior = {};
245- //
246- //// fn.length == arguments.length
247- //
248- //function addMethod(object, name, fn) {
249- //
250- //}
251- //
252- //addMethod(junior, 'ok', function() {
253- // console.log('zero arguments');
254- //});
255- //addMethod(junior, 'ok', function(one) {
256- // console.log('one arguments');
257- //});
258- //addMethod(junior, 'ok', function(one, two) {
259- // console.log('two arguments');
260- //});
261- //addMethod(junior, 'ok', function(one, two, three) {
262- // console.log('three arguments');
263- //});
264- //
265- //junior.ok(1, 2, 3); // 'three arguments'
266- //junior.ok(1, 2); // 'two arguments'
267- //junior.ok(1); //'one arguments'
268- //junior.ok(); //'zero arguments'
244+ let junior = { } ;
245+
246+ // fn.length == arguments.length
247+
248+ function addMethod ( object , name , fn ) {
249+
250+ if ( fn . length === 0 ) {
251+ object . zeroArguments = fn ;
252+ }
253+ if ( fn . length === 1 ) {
254+ object . oneArguments = fn ;
255+ }
256+ if ( fn . length === 2 ) {
257+ object . twoArguments = fn ;
258+ }
259+ if ( fn . length === 3 ) {
260+ object . threeArguments = fn ;
261+ }
262+
263+ object [ name ] = ramification ;
264+ }
265+
266+ function ramification ( ...args ) {
267+ if ( args . length === 0 ) {
268+ return this . zeroArguments ( )
269+ }
270+ if ( args . length === 1 ) {
271+ return this . oneArguments ( )
272+ }
273+ if ( args . length === 2 ) {
274+ return this . twoArguments ( )
275+ }
276+ if ( args . length === 3 ) {
277+ return this . threeArguments ( )
278+ }
279+ }
280+
281+ addMethod ( junior , 'ok' , function ( ) {
282+ console . log ( 'zero arguments' ) ;
283+ } ) ;
284+ addMethod ( junior , 'ok' , function ( one ) {
285+ console . log ( 'one arguments' ) ;
286+ } ) ;
287+ addMethod ( junior , 'ok' , function ( one , two ) {
288+ console . log ( 'two arguments' ) ;
289+ } ) ;
290+ addMethod ( junior , 'ok' , function ( one , two , three ) {
291+ console . log ( 'three arguments' ) ;
292+ } ) ;
293+
294+ junior . ok ( 1 , 2 , 3 ) ; // 'three arguments'
295+ junior . ok ( 1 , 2 ) ; // 'two arguments'
296+ junior . ok ( 1 ) ; //'one arguments'
297+ junior . ok ( ) ; //'zero arguments'
0 commit comments