Created
August 21, 2011 09:22
-
-
Save joseanpg/1160381 to your computer and use it in GitHub Desktop.
GEJS-4 memorizaParametro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.memorizaParametro = function(parametro) { | |
var funcionSobreLaQueSeEjecuto_memorizaParametro = this; | |
return function() { | |
var argsArray = Array.prototype.slice.call(arguments,1); //Menos el primero | |
argsArray.unshift(parametro); | |
return funcionSobreLaQueSeEjecuto_memorizaParametro.apply(this,argsArray); | |
} | |
} | |
//Primer ejemplo: uso puramente funcional | |
var alfa = function(uno,dos,tres) { | |
var r = uno+dos+tres; | |
console.log("Primer parametro: "+uno) | |
console.log("Resultado: "+r); | |
return r; | |
} | |
var beta = alfa.memorizaParametro(7); | |
alfa(1,2,3); //Primer parametro 1, 6 | |
beta(1,2,3); //Primer parametro 7, 12 | |
//Segundo ejemplo: "metodo" de un objeto (usando this) | |
var bob = { | |
nombre:'Bob Esponja', | |
cocinar:function(receta,numero){ | |
var texto = '\u00A1 Yo '+this.nombre+' voy a preparar '+numero+' '+receta+' !'; | |
console.log(texto);return texto; | |
} | |
} | |
bob.cocinar('pizzas',20); //¡ Yo Bob Esponja voy a preparar 20 pizzas ! | |
console.log('\u00A1 Ese no es tu trabajo chico !'); | |
bob.cocinar = bob.cocinar.memorizaParametro('cangreburguers'); | |
bob.cocinar('pizzas',20); //¡ Yo Bob Esponja voy a preparar 20 cangreburguers ! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment