Skip to content

Instantly share code, notes, and snippets.

@igmoweb
Created March 16, 2015 20:28
Show Gist options
  • Save igmoweb/f0ba79562db42b8d3e9e to your computer and use it in GitHub Desktop.
Save igmoweb/f0ba79562db42b8d3e9e to your computer and use it in GitHub Desktop.
Arguments
// extend the Function object to include a wrap instance method
Function.prototype.wrap = function( callback ) {
var original = this;
// Aquí arguments tiene un sólo elemento (la función anónima que se pasa a speak)
console.log(arguments);
return function() {
// Aquí arguments tiene 23 elementos (¿Por qué?). Son 'Mary' y 'Kate'
// Si estamos en una función anónima sin parámetros,¿No debería estar vacío?
console.log(arguments);
callback.apply( original, [original].concat( Array.prototype.slice.call( arguments, 0 ) ) );
}
}
function speak(name){
return "Hello " + name;
}
speak = speak.wrap(function(original, yourName, myName){
greeting = original(yourName);
return greeting + ", my name is " + myName;
})
var greeting = speak("Mary", "Kate");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment