File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Roadmap/24 - DECORADORES/javascript Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ // ** EJERCICIO
2+
3+ function timeLogger ( targetFunction ) {
4+ return function ( ...args ) {
5+ console . time ( 'Execution Time' ) ;
6+ const result = targetFunction . apply ( this , args ) ;
7+ console . timeEnd ( 'Execution Time' ) ;
8+ return result ;
9+ } ;
10+ }
11+
12+ function sayHello ( name ) { // Función original
13+ console . log ( `Hola, ${ name } !` ) ;
14+ }
15+
16+ const timedSayHello = timeLogger ( sayHello ) ; // Decorar la función
17+
18+ //timedSayHello('Pepe'); // Llamar a la función decorada
19+
20+ // ** DIFICULTAD EXTRA ** ------------------------------------------------------------------------------------------------------------------------------------------------
21+
22+
23+ function timeCounter ( targetFunction ) {
24+ let functionCount = 0
25+
26+ return function ( ...args ) {
27+ const result = targetFunction . apply ( this , args ) ;
28+ functionCount ++
29+ console . log ( 'La función sayHello se ha ejecutado ' + functionCount + ' veces.' )
30+ } ;
31+ }
32+ function sayHello ( name ) { // Función original
33+ console . log ( `Hola, ${ name } !` ) ;
34+ }
35+ const countedSayHello = timeCounter ( sayHello )
36+
37+ countedSayHello ( 'Juan' )
38+ countedSayHello ( 'María' )
39+ countedSayHello ( 'Julián' )
You can’t perform that action at this time.
0 commit comments