jquery using object literal over multiple files

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.

Answers:

Answer

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 () {}
Answer

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 ;)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.