Created
March 16, 2015 20:28
-
-
Save igmoweb/f0ba79562db42b8d3e9e to your computer and use it in GitHub Desktop.
Arguments
This file contains hidden or 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
// 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