unidades:01_introduccion:04_repasojs
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| unidades:01_introduccion:04_repasojs [2014/09/06 14:58] – admin | unidades:01_introduccion:04_repasojs [2014/09/07 17:38] (actual) – [Closure] admin | ||
|---|---|---|---|
| Línea 33: | Línea 33: | ||
| <sxh js> | <sxh js> | ||
| function Persona() { | function Persona() { | ||
| - | this.nombre=" | + | |
| - | this.ape1=" | + | this.ape1=" |
| - | this.edad=34; | + | this.edad=34; |
| } | } | ||
| Línea 71: | Línea 71: | ||
| <sxh js> | <sxh js> | ||
| function Persona() { | function Persona() { | ||
| - | this.nombre=" | + | |
| - | this.ape1=" | + | this.ape1=" |
| - | this.edad=34; | + | this.edad=34; |
| - | this.getNombreCompleto=function() { | + | this.getNombreCompleto=function() { |
| return this.nombre+this.ape1; | return this.nombre+this.ape1; | ||
| - | }; | + | |
| } | } | ||
| Línea 89: | Línea 89: | ||
| <sxh js> | <sxh js> | ||
| function sumar(a,b) { | function sumar(a,b) { | ||
| - | return a+b; | + | |
| } | } | ||
| Línea 98: | Línea 98: | ||
| <sxh js> | <sxh js> | ||
| var sumar=function (a,b) { | var sumar=function (a,b) { | ||
| - | return a+b; | + | |
| } | } | ||
| Línea 104: | Línea 104: | ||
| </ | </ | ||
| + | * Podemos hacer funciones que retornan funciones y llamarlas directamente. | ||
| + | |||
| + | <sxh js> | ||
| + | function getFunction() { | ||
| + | |||
| + | var sumar=function (a,b) { | ||
| + | return a+b; | ||
| + | } | ||
| + | |||
| + | return sumar; | ||
| + | } | ||
| + | |||
| + | var c=getFunction()(1, | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Closure ===== | ||
| + | Ahora vamos a ver una de las cosas mas raras para un programador Java. Una closure. Inicialmente es como un simple puntero a una función, algo como lo que ya existía en C, pero hay algo más. Veamos el siguiente ejemplo: | ||
| + | |||
| + | <sxh js; | ||
| + | function getFunction() { | ||
| + | |||
| + | var extra=100; | ||
| + | |||
| + | var sumar=function (a,b) { | ||
| + | return a+b+extra; | ||
| + | } | ||
| + | |||
| + | return sumar; | ||
| + | } | ||
| + | |||
| + | var sumaClosure=getFunction(); | ||
| + | |||
| + | var c=sumaClosure(1, | ||
| + | |||
| + | </ | ||
| + | |||
| + | Al ejecutar el ejemplo , la variable '' | ||
| + | |||
| + | El ejemplo lo podemos liar un poco mas añadiendo nuevas variables llamadas '' | ||
| + | |||
| + | <sxh js; | ||
| + | var extra=1000; | ||
| + | |||
| + | function getFunction() { | ||
| + | |||
| + | var extra=100; | ||
| + | |||
| + | var sumar=function (a,b) { | ||
| + | return a+b+extra; | ||
| + | } | ||
| + | |||
| + | return sumar; | ||
| + | } | ||
| + | |||
| + | extra=1100; | ||
| + | |||
| + | var fn=getFunction(); | ||
| + | |||
| + | extra=1200; | ||
| + | |||
| + | var c=fn(1,2); | ||
| + | </ | ||
unidades/01_introduccion/04_repasojs.1410008309.txt.gz · Última modificación: por admin
