I want to define several methods from an array of method names like so:
var methodNames = ['foo', 'bar', 'baz'],
randomObject = {}, method;
for( var i = methodNames.length - 1; i >= 0; --i ){
method = methodNames[ i ];
randomObject[ method ] = function(){
console.log( method );
}
}
So that I end up with an object randomObject
, which has all the methods defined doing the exact same thing. The problem is, every method logs 'foo' instead of the name of the method being called. How can I make the variable method
persist when the method is called?