File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Roadmap/24 - DECORADORES/javascript Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * EJERCICIO:
3+ * Explora el concepto de "decorador" y muestra cómo crearlo
4+ * con un ejemplo genérico.
5+ */
6+ class Math {
7+ add ( a , b ) {
8+ return a + b ;
9+ }
10+ }
11+
12+ function decoratorLog ( f ) {
13+ let oldValue = f ;
14+
15+ f = function ( ) {
16+ console . log ( `Calling "${ f . name } " with` , arguments ) ;
17+ return oldValue . apply ( null , arguments ) ;
18+ } ;
19+ }
20+
21+ Math . prototype . add = decoratorLog ( Math . prototype . add ) ;
22+
23+ /* DIFICULTAD EXTRA (opcional):
24+ * Crea un decorador que sea capaz de contabilizar cuántas veces
25+ * se ha llamado a una función y aplícalo a una función de tu elección.
26+ */
27+
28+ function contadorLlamadas ( fn ) {
29+ let contador = 0 ;
30+
31+ return function ( ...args ) {
32+ contador ++ ;
33+ console . log ( `La función ha sido llamada ${ contador } veces` ) ;
34+
35+ return fn . apply ( this , args ) ;
36+ } ;
37+ }
38+
39+ function saludo ( nombre ) {
40+ console . log ( `Hola, ${ nombre } !` ) ;
41+ }
42+
43+ const saludoConContador = contadorLlamadas ( saludo ) ;
44+
45+ saludoConContador ( "Hernan" ) ;
46+ saludoConContador ( "Agustin" ) ;
You can’t perform that action at this time.
0 commit comments