Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created August 21, 2011 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/1160381 to your computer and use it in GitHub Desktop.
Save joseanpg/1160381 to your computer and use it in GitHub Desktop.
GEJS-4 memorizaParametro
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