Chapter 7. Customizing Titanium

The previous chapters have given you an overview of Titanium’s language, namespaces, objects, and methods, hopefully offering you plenty of neat stuff to work with. But Titanium’s foundation in JavaScript also makes it easy to extend and customize. JavaScript lets you create your own prototype functions that add functionality to any class you want. For instance, you can easily create a string prototype function that adds functionality to any string object created in JavaScript, like this:

String.prototype.trim = function() { 
    return this.replace(/^\s+|\s+$/, ''); };

This line of code adds a trim function to any JavaScript string object you create. Prototypes are a very cool feature of JavaScript and should be exploited profusely. They allow you to add functions that perhaps you’ve gotten used to in other languages and would like to take advantage of while working with Titanium.

You can also make “mini-factories,” which are basically functions that create Titanium objects with certain properties or other traits that you’ll need regularly. Creating these mini-factories will allow you to have a central point where you can make changes, and all the calls to these factories will return an object with those changes in it.

What’s in a Name...Space

Titanium contains many separate objects, making them available via namespaces. In general terms, a namespace is an abstract container that provides a context for the items that it contains.

The topmost object in the Titanium ...

Get Appcelerator Titanium: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.