Javascript error: * is not a function

firefoxfunctionjavascript

I'm using javascript to call functions that have been put into an object like so:

generatewidgets['mywidget'] = function (mystring) { code }

Later on, i loop through this object and call each function with a string parameter.

argument = 'abcdefg';
for (this.key in generatewidgets)
    generatewidgets[this.key](argument);

This works fine in IE8, but in firefox throws an error "argument is not a function". It does this with any variable name i specify for the string. I've also tried:

generatewidgets[this.key](argument+'');

which results in "'abcdefg' is not a function". Anyone know what this error means or why I'm getting it?

Best Answer

You can also check the type before the call:

for (var key in generatewidgets) {
  if (typeof generatewidgets[key] === 'function')
    generatewidgets[key](argument);
}

This is probably a good idea anyway in case somewhere down the road you ever want to add a non-function item to the object.