Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 11:42
Show Gist options
  • Save bennadel/9760100 to your computer and use it in GitHub Desktop.
Save bennadel/9760100 to your computer and use it in GitHub Desktop.
Invoking Javascript Methods With Both Named And Ordered Arguments
<!DOCTYPE html>
<html lang="en">
<head>
<title>Invoking Javascript Methods With Named Arguments</title>
</head>
<body>
<h1>
Invoking Javascript Methods With Named Arguments
</h1>
<script type="text/javascript">
(function(){
// I take the given function [as a string] and extract
// the arguments as a named-argument map. This will
// return the map as an array of ordered names.
function extractArgumentMap( functionCode ){
// Extract the argument string.
var argumentStringMatch = functionCode.match(
new RegExp( "\\([^)]*\\)", "" )
);
// Now, extract the arguments.
var argumentMap = argumentStringMatch[ 0 ].match(
new RegExp( "[^\\s,()]+", "g" )
);
// Return the argument map.
return( argumentMap );
}
// I allow the current method (this) to be executed
// using a named-argument map rathre than ordered
// arguments. Any non-provided arguments will be null
// for method execution.
Function.prototype.invoke = function( context, namedArguments ){
// Check to see if the arguments have been mapped for
// this method yet.
if (!("argumentMap" in this)){
// Extract and store the argument map. We need to
// pass in the target method (this) as a string
// in order to extract the argument map.
this.argumentMap = extractArgumentMap(
this.toString()
);
}
// Create an array for our invocation arguments.
var orderedArguments = [];
// Now, let's loop over the argument map to move the
// named arguments over to the apply arguments.
for (var i = 0 ; i < this.argumentMap.length ; i++){
// Check to see if the named-argument was
// provided by the caller.
if (this.argumentMap[ i ] in namedArguments){
// Map the named-argument to the ordered
// argument.
orderedArguments.push(
namedArguments[ this.argumentMap[ i ] ]
);
} else {
// The named-argument was not provided. Just
// add null for invocation.
orderedArguments.push( null );
}
}
// Invoke the target argument (this) in the given
// context and return the result.
return(
this.apply( context, orderedArguments )
);
};
})();
// -------------------------------------------------- //
// -------------------------------------------------- //
// -------------------------------------------------- //
// -------------------------------------------------- //
// Define a Sarah object.
var sarah = {
name: "Sarah",
sayHello: function( name, compliment ){
return(
"Hi " + name + ", I'm " + this.name + ". " +
"You're so sweet to say that I'm " +
compliment + "."
);
}
};
// -------------------------------------------------- //
// -------------------------------------------------- //
// Invoke sarah's sayHello() method using named arguments.
var response = sarah.sayHello.invoke(
sarah,
{
compliment: "wicked hot",
name: "Ben"
}
);
// Output the result.
console.log( response );
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment