New in Titanium 3.2.0: apiName

Continuing my series of blogs on new features in the upcoming Titanium 3.2.0 (and Alloy 1.3.0) I present to you a simple but nice new property. All Titanium SDK objects. now expose their class via the new apiName property. So if you would create a button and then log myButton.apiName it would tell you it’s a Ti.UI.Button.

Prototype vs Class

One of the biggest misconceptions about JavaScript is that it would have classes like Objective-C, JAVA, PHP and many more do. Instead, JavaScript is a prototype-based language, not a class-based one. Douglas Crockford, author of JavaScript: The Good Parts wrote some good articles explaining the difference.

How to get a object’s class?

One of the drawbacks of JavaScript not using classes shows when you take a look at the answers to How to get a JavaScript Object’s Class? on Stack Overflow.

Let’s try some of the answers given:

var btn = Ti.UI.createButton();

console.log('toString: ' + btn);
console.log('constructor: ' + btn.constructor);
console.log('instanceof: ' + (btn instanceof Ti.UI.Button ? 'Yes' : 'No'));
console.log('isPrototypeOf: ' + (Ti.UI.Button.prototype.isPrototypeOf(btn) ? 'Yes' : 'No'));

If you would run this code you would see that the last 2 crash the app telling you Ti.UI.Button is undefined, while the second one logs exactly that. These have to do with the Titanium objects not being 100% pure JavaScript, but as you know bridge to native objects. Only the first one works and logs [object TiUIButton]. Not exactly readable so I hope you like the new property as much as I do.

App imagineer: Imagining, Engineering & Speaking about Native mobile Apps with Appcelerator Titanium & Alloy • Meetup organizer • Certified Expert • Titan


  • Simon Buckingham

    Would be nice if one could do this with widgets (ie it would return the apiName of the top view). Can’t seem to find a way of testing the widgets view type except by exporting it from within the widget.