I would suggest using bind
:
fields.forEach( function(field) {
functions[field.name] = field.doSomething.bind(field);
}
Otherwise, there is no chance that the value of this
inside your doSomething
is going to be what you want. Calling bind
sets this
to the value of field
at the time bind
is called.