Global variables
Global variables are very easy to create in Titanium. We have already seen many of them in the book so far. Any variable that is not enclosed within a function is likely to be a global variable, so many of the variables defined in app.js
will be global. Variables need to be enclosed in a function or a closure in order for them to be constrained. Global variables will exist for as long as your app runs, which can use valuable resources. An example of a closure is shown in the first example in this chapter.
What is the right way to share data?
There are a couple of methods for sharing data:
Method |
Advantages |
Disadvantages |
---|---|---|
Global variable |
It is easy to define. |
Cannot be garbage collected. The data can hang around for as long as the app is open. |
Creating an application property using
|
It is persistent. |
Slow and unpractical for anything other than small amounts of data. Slower than the global variable method. |
Filesystem |
It is persistent. |
Only practical... |