views:

48

answers:

1

Currently in a simplified form, my code looks like this

function AddFileParam(file_id, name, value) {
    uploadcontrol.AddFileParam(file_id, name, value)
}

uploadcontrol = new Upload()

function Upload() {
    //logic
}
Upload.prototype.AddFileParam = function(file_id, name, value) {
    //logic
};

The code is giving me an error as it states that addFileParam is not a valid function. This is caused by the instance of the function upload (aka. uploadcontrol). This is only occuring in Firefox/Chrome and not in IE. Any ideas on how to fix this?

A: 

If the global AddFileParam() function gets called before execution reaches the Upload.prototype.AddFileParam= ... line then the Upload prototype will not yet have an AddFileParam method.

JavaScript executes from top to bottom. The exception is that the function declaration statements like function Upload() {...} are ‘hoisted’ and defined before the rest of the script is run. The Upload.prototype.member= line following that constructor function, though, is not hoisted, and runs in normal script order.

Though you can get away with using a function earlier in a script than its definition, it's not a good idea to do so for constructor functions.

bobince