views:

23

answers:

2

I've seen object literal examples and they look really nice and sensible. What I want to know is if it's possible to use object literal over multiple files, because all the examples I've seen had only 1 file.

Thanks.

A: 

If you mean in web browsers, file scope is meaningless (unless you use WebWorkers, but I doubt you're at that stage yet). When you declare a global variable, it's available to all .js files you include in your html page. It doesn't matter if it's an object literal, function, string or whatever.

Functions/methods however do have their own scope, so if you declare variables inside a function, it's not global and thus not available to other code.

If that does not awnser your question, please be a bit more specific ;)

BGerrissen
I'm asking about adding more functions to the same master object in a different file. That way I can have init in one file, run in a different file, etc.
JSNewbie
A: 

At the top of each file you check to see if the object already exists.. if it does, then use the existing object.. if not then create a new object so you're not trying to work on an undefined variable.

var object = object || {}

object.doStuff = function () {}
CD Sanchez