@maccesar/titools 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS-TEMPLATE.md +173 -0
- package/README.md +867 -0
- package/agents/ti-researcher.md +108 -0
- package/bin/titools.js +53 -0
- package/lib/commands/agents.js +126 -0
- package/lib/commands/install.js +188 -0
- package/lib/commands/uninstall.js +215 -0
- package/lib/commands/update.js +159 -0
- package/lib/config.js +119 -0
- package/lib/downloader.js +153 -0
- package/lib/installer.js +253 -0
- package/lib/platform.js +108 -0
- package/lib/symlink.js +142 -0
- package/lib/utils.js +270 -0
- package/package.json +67 -0
- package/skills/alloy-expert/SKILL.md +247 -0
- package/skills/alloy-expert/assets/ControllerAutoCleanup.js +182 -0
- package/skills/alloy-expert/references/alloy-structure.md +381 -0
- package/skills/alloy-expert/references/anti-patterns.md +133 -0
- package/skills/alloy-expert/references/code-conventions.md +469 -0
- package/skills/alloy-expert/references/contracts.md +280 -0
- package/skills/alloy-expert/references/controller-patterns.md +520 -0
- package/skills/alloy-expert/references/error-handling.md +484 -0
- package/skills/alloy-expert/references/examples.md +735 -0
- package/skills/alloy-expert/references/migration-patterns.md +298 -0
- package/skills/alloy-expert/references/patterns.md +448 -0
- package/skills/alloy-expert/references/performance-patterns.md +855 -0
- package/skills/alloy-expert/references/security-patterns.md +847 -0
- package/skills/alloy-expert/references/state-management.md +779 -0
- package/skills/alloy-expert/references/testing.md +872 -0
- package/skills/alloy-guides/SKILL.md +214 -0
- package/skills/alloy-guides/references/CLI_TASKS.md +243 -0
- package/skills/alloy-guides/references/CONCEPTS.md +191 -0
- package/skills/alloy-guides/references/CONTROLLERS.md +298 -0
- package/skills/alloy-guides/references/MODELS.md +1028 -0
- package/skills/alloy-guides/references/PURGETSS.md +56 -0
- package/skills/alloy-guides/references/VIEWS_DYNAMIC.md +242 -0
- package/skills/alloy-guides/references/VIEWS_STYLES.md +388 -0
- package/skills/alloy-guides/references/VIEWS_WITHOUT_CONTROLLERS.md +109 -0
- package/skills/alloy-guides/references/VIEWS_XML.md +558 -0
- package/skills/alloy-guides/references/WIDGETS.md +176 -0
- package/skills/alloy-howtos/SKILL.md +203 -0
- package/skills/alloy-howtos/references/best_practices.md +138 -0
- package/skills/alloy-howtos/references/cli_reference.md +253 -0
- package/skills/alloy-howtos/references/config_files.md +87 -0
- package/skills/alloy-howtos/references/custom_tags.md +147 -0
- package/skills/alloy-howtos/references/debugging_troubleshooting.md +101 -0
- package/skills/alloy-howtos/references/samples.md +167 -0
- package/skills/purgetss/SKILL.md +442 -0
- package/skills/purgetss/assets/purgetss.config.cjs +17 -0
- package/skills/purgetss/references/EXAMPLES.md +247 -0
- package/skills/purgetss/references/animation-system.md +1294 -0
- package/skills/purgetss/references/apply-directive.md +375 -0
- package/skills/purgetss/references/arbitrary-values.md +612 -0
- package/skills/purgetss/references/class-index.md +1350 -0
- package/skills/purgetss/references/cli-commands.md +948 -0
- package/skills/purgetss/references/configurable-properties.md +654 -0
- package/skills/purgetss/references/custom-rules.md +161 -0
- package/skills/purgetss/references/customization-deep-dive.md +722 -0
- package/skills/purgetss/references/dynamic-component-creation.md +489 -0
- package/skills/purgetss/references/grid-layout.md +455 -0
- package/skills/purgetss/references/icon-fonts.md +609 -0
- package/skills/purgetss/references/installation-setup.md +366 -0
- package/skills/purgetss/references/opacity-modifier.md +291 -0
- package/skills/purgetss/references/platform-modifiers.md +479 -0
- package/skills/purgetss/references/smart-mappings.md +42 -0
- package/skills/purgetss/references/titanium-resets.md +359 -0
- package/skills/purgetss/references/ui-ux-design.md +1526 -0
- package/skills/ti-guides/SKILL.md +94 -0
- package/skills/ti-guides/references/advanced-data-and-images.md +19 -0
- package/skills/ti-guides/references/alloy-cli-advanced.md +84 -0
- package/skills/ti-guides/references/alloy-data-mastery.md +29 -0
- package/skills/ti-guides/references/alloy-widgets-and-themes.md +19 -0
- package/skills/ti-guides/references/android-manifest.md +97 -0
- package/skills/ti-guides/references/app-distribution.md +258 -0
- package/skills/ti-guides/references/application-frameworks.md +377 -0
- package/skills/ti-guides/references/cli-reference.md +402 -0
- package/skills/ti-guides/references/coding-best-practices.md +102 -0
- package/skills/ti-guides/references/commonjs-advanced.md +134 -0
- package/skills/ti-guides/references/hello-world.md +100 -0
- package/skills/ti-guides/references/hyperloop-native-access.md +62 -0
- package/skills/ti-guides/references/javascript-primer.md +411 -0
- package/skills/ti-guides/references/reserved-words.md +36 -0
- package/skills/ti-guides/references/resources.md +183 -0
- package/skills/ti-guides/references/style-and-conventions.md +48 -0
- package/skills/ti-guides/references/tiapp-config.md +609 -0
- package/skills/ti-howtos/SKILL.md +174 -0
- package/skills/ti-howtos/references/android-platform-deep-dives.md +658 -0
- package/skills/ti-howtos/references/automation-fastlane-appium.md +95 -0
- package/skills/ti-howtos/references/buffer-codec-streams.md +140 -0
- package/skills/ti-howtos/references/cross-platform-development.md +348 -0
- package/skills/ti-howtos/references/debugging-profiling.md +543 -0
- package/skills/ti-howtos/references/extending-titanium.md +723 -0
- package/skills/ti-howtos/references/google-maps-v2.md +169 -0
- package/skills/ti-howtos/references/ios-map-kit.md +143 -0
- package/skills/ti-howtos/references/ios-platform-deep-dives.md +783 -0
- package/skills/ti-howtos/references/local-data-sources.md +301 -0
- package/skills/ti-howtos/references/location-and-maps.md +252 -0
- package/skills/ti-howtos/references/media-apis.md +210 -0
- package/skills/ti-howtos/references/notification-services.md +599 -0
- package/skills/ti-howtos/references/remote-data-sources.md +349 -0
- package/skills/ti-howtos/references/tutorials.md +502 -0
- package/skills/ti-howtos/references/using-modules.md +237 -0
- package/skills/ti-howtos/references/web-content-integration.md +307 -0
- package/skills/ti-howtos/references/webpack-build-pipeline.md +78 -0
- package/skills/ti-ui/SKILL.md +179 -0
- package/skills/ti-ui/references/accessibility-deep-dive.md +242 -0
- package/skills/ti-ui/references/animation-and-matrices.md +599 -0
- package/skills/ti-ui/references/application-structures.md +655 -0
- package/skills/ti-ui/references/custom-fonts-styling.md +579 -0
- package/skills/ti-ui/references/event-handling.md +393 -0
- package/skills/ti-ui/references/gestures.md +473 -0
- package/skills/ti-ui/references/icons-and-splash-screens.md +409 -0
- package/skills/ti-ui/references/layouts-and-positioning.md +462 -0
- package/skills/ti-ui/references/listviews-and-performance.md +619 -0
- package/skills/ti-ui/references/orientation.md +362 -0
- package/skills/ti-ui/references/platform-ui-android.md +635 -0
- package/skills/ti-ui/references/platform-ui-ios.md +469 -0
- package/skills/ti-ui/references/scrolling-views.md +252 -0
- package/skills/ti-ui/references/tableviews.md +568 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Hello World - Project Creation
|
|
2
|
+
|
|
3
|
+
Quick guide to creating your first Titanium project.
|
|
4
|
+
|
|
5
|
+
## Creating a New Project
|
|
6
|
+
|
|
7
|
+
### Using CLI
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
ti create -t app --id <APP_ID> -n <APP_NAME> -p <PLATFORMS> -d <WORKSPACE>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**Example:**
|
|
14
|
+
```bash
|
|
15
|
+
ti create -t app --id com.example.hello -n HelloWorld -p android,ios -d ~/Projects
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Using Studio/VS Code
|
|
19
|
+
|
|
20
|
+
**File** > **New** > **Mobile App Project**
|
|
21
|
+
|
|
22
|
+
Choose template:
|
|
23
|
+
- **Alloy** - MVC framework (recommended)
|
|
24
|
+
- **Classic** - No framework
|
|
25
|
+
|
|
26
|
+
## Project Fields
|
|
27
|
+
|
|
28
|
+
| Field | Description | Rules |
|
|
29
|
+
| ---------------------- | ----------------------- | -------------------------- |
|
|
30
|
+
| **Project name** | App name shown to users | - |
|
|
31
|
+
| **App ID** | Reverse domain notation | `com.company.appname` |
|
|
32
|
+
| **Company URL** | Your website | - |
|
|
33
|
+
| **SDK Version** | Titanium SDK to use | Use latest |
|
|
34
|
+
| **Deployment Targets** | Platforms to support | android, ios, ipad, iphone |
|
|
35
|
+
|
|
36
|
+
### App ID Naming Guidelines
|
|
37
|
+
|
|
38
|
+
- Use Java Package Name style: `com.yourdomain.yourappname`
|
|
39
|
+
- No spaces or special characters
|
|
40
|
+
- All lowercase (Android issues with uppercase)
|
|
41
|
+
- No Java keywords (`case`, `package`, etc.)
|
|
42
|
+
- Cannot change after publishing
|
|
43
|
+
|
|
44
|
+
## Project Structure
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
MyApp/
|
|
48
|
+
├── app/ # Alloy app source
|
|
49
|
+
│ ├── assets/ # Images, fonts
|
|
50
|
+
│ ├── controllers/ # JS controllers
|
|
51
|
+
│ ├── models/ # Backbone models
|
|
52
|
+
│ ├── views/ # XML views
|
|
53
|
+
│ └── styles/ # TSS styles
|
|
54
|
+
├── platform/ # Platform-specific files
|
|
55
|
+
│ ├── android/
|
|
56
|
+
│ └── ios/
|
|
57
|
+
├── Resources/ # Classic Titanium resources
|
|
58
|
+
├── i18n/ # Internationalization
|
|
59
|
+
├── tiapp.xml # App configuration
|
|
60
|
+
├── config.json # Alloy config
|
|
61
|
+
└── app.js # Classic entry point
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Running Your App
|
|
65
|
+
|
|
66
|
+
### iOS Simulator
|
|
67
|
+
```bash
|
|
68
|
+
ti build -p ios -C "iPhone 15"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Android Emulator
|
|
72
|
+
```bash
|
|
73
|
+
ti build -p android -C "Pixel_4_API_34"
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Physical Device
|
|
77
|
+
```bash
|
|
78
|
+
ti build -p ios -T device
|
|
79
|
+
ti build -p android -T device
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Simulator vs Emulator
|
|
83
|
+
|
|
84
|
+
- **iOS Simulator**: The software simulates the environment within an iOS device. It's an OS X executable that runs your cross-compiled code.
|
|
85
|
+
- **Android Emulator**: Provides a virtual hardware environment that runs the actual Android OS and platform components.
|
|
86
|
+
|
|
87
|
+
**CRITICAL**: Neither environment is a perfect representation of a physical device. Always test on real hardware before publishing.
|
|
88
|
+
|
|
89
|
+
## How Titanium Works (Under the Covers)
|
|
90
|
+
|
|
91
|
+
1. **Pre-compile**: JavaScript is minified and statically analyzed to build a dependency hierarchy of Titanium APIs used.
|
|
92
|
+
2. **Stub Generation**: A front-end compiler creates native stub files, native project files, and platform-specific code necessary for compilation.
|
|
93
|
+
3. **Native Build**: Titanium calls platform-specific compilers (e.g., `xcodebuild` for iOS, Gradle for Android) to build the final native application.
|
|
94
|
+
4. **Encryption**: JavaScript code is encrypted when building for "production" (release) or for device. Original code is not retrievable in human-readable form.
|
|
95
|
+
|
|
96
|
+
## Best Practices
|
|
97
|
+
|
|
98
|
+
1. **Always test on physical devices** - Simulator/emulator isn't perfect
|
|
99
|
+
2. **Use Alloy for new projects** - Better structure and maintainability
|
|
100
|
+
3. **Keep App ID consistent** - Match Bundle ID (iOS) and Package ID (Android)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Hyperloop: Native API Access
|
|
2
|
+
|
|
3
|
+
Hyperloop allows you to access 100% of native APIs (iOS Objective-C/Swift and Android Java/Kotlin) directly from JavaScript.
|
|
4
|
+
|
|
5
|
+
## 1. Core Syntax
|
|
6
|
+
- **`require('Framework/Class')`**: Access the native class.
|
|
7
|
+
- **Instantiation**: Use `new Class()` or `Class.alloc().init()`.
|
|
8
|
+
|
|
9
|
+
## 2. iOS (Objective-C)
|
|
10
|
+
- **Method Concatenation**: Selector `addAttribute:value:range:` becomes `addAttributeValueRange(a, v, r)`.
|
|
11
|
+
- **Casting**: Use `UIView.cast(proxy)` to convert a Titanium proxy to a native view.
|
|
12
|
+
- **Blocks**: Pass a JavaScript function where a native block is expected.
|
|
13
|
+
|
|
14
|
+
## 3. Android (Java)
|
|
15
|
+
- **Activity Context**: Often required for view constructors.
|
|
16
|
+
```javascript
|
|
17
|
+
const Activity = require('android.app.Activity');
|
|
18
|
+
const activity = new Activity(Ti.Android.currentActivity);
|
|
19
|
+
```
|
|
20
|
+
- **Interfaces**: Pass a JS object with methods matching the interface.
|
|
21
|
+
- **Gradle**: Add dependencies in `app/platform/android/build.gradle`.
|
|
22
|
+
- **Method Overloading**: Hyperloop automatically selects the matching overload based on parameter types.
|
|
23
|
+
|
|
24
|
+
## 4. Swift Support (iOS)
|
|
25
|
+
Hyperloop supports Swift classes alongside Objective-C. You can directly access Swift classes from JavaScript.
|
|
26
|
+
- Swift and Objective-C classes can be mixed in the same app
|
|
27
|
+
- Use the same `require()` syntax for Swift classes
|
|
28
|
+
|
|
29
|
+
## 5. XIB and Storyboards (iOS)
|
|
30
|
+
XIB files and Storyboards are supported but must be programmatically loaded:
|
|
31
|
+
```javascript
|
|
32
|
+
// Load XIB example
|
|
33
|
+
const NSBundle = require('Foundation.NSBundle');
|
|
34
|
+
const UINib = require('UIKit.UINib');
|
|
35
|
+
|
|
36
|
+
const nib = UINib.nibWithNibName_bundle('MyView', NSBundle.mainBundle);
|
|
37
|
+
const objects = nib.instantiateWithOwner_options(null, null);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 6. Debugging Hyperloop
|
|
41
|
+
|
|
42
|
+
### Limitations
|
|
43
|
+
- Studio debugging capabilities are limited for Hyperloop code
|
|
44
|
+
- Hyperloop modifies source files during build, which throws debuggers off track
|
|
45
|
+
- Breakpoints may not hit correctly in main listview click events
|
|
46
|
+
|
|
47
|
+
### Workarounds
|
|
48
|
+
- **iOS**: Use Safari Web Inspector for debugging up to the native transition point
|
|
49
|
+
- **Android**: Use Chrome DevTools (SDK 7.0.0+)
|
|
50
|
+
- Note: No source maps available for processed Alloy controllers
|
|
51
|
+
|
|
52
|
+
## 7. TiApp Utility Class (iOS)
|
|
53
|
+
The `TiApp` utility class provides access to Titanium's app instance:
|
|
54
|
+
```javascript
|
|
55
|
+
const TiApp = require('TiApp.TiApp');
|
|
56
|
+
const app = TiApp.app();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Useful for accessing the app delegate, key window, and app-level properties.
|
|
60
|
+
|
|
61
|
+
## 8. Performance Tip
|
|
62
|
+
Don't use Hyperloop for things Titanium already does well. Use it for specialized APIs (e.g., custom UI components or hardware access not covered by modules).
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
# JavaScript Development Primer
|
|
2
|
+
|
|
3
|
+
Essential JavaScript concepts, resources, and best practices for Titanium development.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
1. [JavaScript Overview](#javascript-overview)
|
|
7
|
+
2. [Learning Resources](#learning-resources)
|
|
8
|
+
3. [Best Practices](#best-practices)
|
|
9
|
+
4. [Common Patterns](#common-patterns)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## JavaScript Overview
|
|
14
|
+
|
|
15
|
+
JavaScript is the language of Titanium. It's a powerful, lightweight, dynamic object-oriented programming language.
|
|
16
|
+
|
|
17
|
+
**Why JavaScript for Titanium?**
|
|
18
|
+
- One of the most widely deployed languages (every web browser)
|
|
19
|
+
- Large community of developers
|
|
20
|
+
- Dynamic typing with duck typing
|
|
21
|
+
- Functional programming support
|
|
22
|
+
- Convenient object literal notation
|
|
23
|
+
- Closures for encapsulation
|
|
24
|
+
- Small learning curve
|
|
25
|
+
|
|
26
|
+
**JavaScript in Titanium:**
|
|
27
|
+
- No DOM manipulation (that's web-specific)
|
|
28
|
+
- Access to native APIs via Titanium namespace
|
|
29
|
+
- ECMAScript 5/6 compliant
|
|
30
|
+
- Same JavaScript you use in web, but targeting mobile apps
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Learning Resources
|
|
35
|
+
|
|
36
|
+
### Online Courses
|
|
37
|
+
|
|
38
|
+
- **[Codecademy](https://www.codecademy.com/learn/introduction-to-javascript)** - Interactive JavaScript tutorials
|
|
39
|
+
- **[Stanford CS101](http://www.stanford.edu/class/cs101/)** - Uses JavaScript, lecture notes and projects available
|
|
40
|
+
- **[edX](https://www.edx.org/learn/javascript)** - Free courses from top universities
|
|
41
|
+
|
|
42
|
+
### Online Books & References
|
|
43
|
+
|
|
44
|
+
- **[Eloquent JavaScript](https://eloquentjavascript.net/)** - Excellent learning resource (free online, or buy the book)
|
|
45
|
+
- **[MDN JavaScript Guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)** - Comprehensive JavaScript documentation
|
|
46
|
+
- **[Douglas Crockford's Resources](http://javascript.crockford.com/)** - From the creator of JSON and JSLint
|
|
47
|
+
- **[JavaScript in 10 Minutes](https://github.com/spencertipping/js-in-ten-minutes)** - Dense advanced guide
|
|
48
|
+
- **[Google JavaScript Style Guide](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml)** - Code style guidelines
|
|
49
|
+
- **[Learning Advanced JavaScript](http://ejohn.org/apps/learn/)** - By John Resig (jQuery creator)
|
|
50
|
+
|
|
51
|
+
### Print Books (Recommended)
|
|
52
|
+
|
|
53
|
+
- **JavaScript: The Good Parts** (Douglas Crockford) - ESSENTIAL
|
|
54
|
+
- **JavaScript: The Definitive Guide** (David Flanagan) - Complete reference
|
|
55
|
+
- **JavaScript Patterns** (Stoyan Stefanov) - Design patterns
|
|
56
|
+
- **Eloquent JavaScript** (Marijn Haverbeke) - Also available free online
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Best Practices
|
|
61
|
+
|
|
62
|
+
### Don't Pollute the Global Scope
|
|
63
|
+
|
|
64
|
+
**BAD:** Everything in global scope
|
|
65
|
+
```javascript
|
|
66
|
+
const key = 'value';
|
|
67
|
+
const foo = 'bar';
|
|
68
|
+
|
|
69
|
+
function helper() {
|
|
70
|
+
// help out
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function info(msg) {
|
|
74
|
+
helper(msg);
|
|
75
|
+
Ti.API.info(msg);
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**GOOD:** Single namespace
|
|
80
|
+
```javascript
|
|
81
|
+
const myapp = {}; // Only ONE global variable
|
|
82
|
+
|
|
83
|
+
myapp.key = 'value';
|
|
84
|
+
|
|
85
|
+
myapp.dosomething = (foo) => {
|
|
86
|
+
// do something
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// Self-closing function for private members
|
|
90
|
+
(() => {
|
|
91
|
+
function helper() {
|
|
92
|
+
// Private - not accessible globally
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
myapp.info = (msg) => {
|
|
96
|
+
helper(msg);
|
|
97
|
+
Ti.API.info(msg);
|
|
98
|
+
};
|
|
99
|
+
})();
|
|
100
|
+
|
|
101
|
+
myapp.info('Hello World');
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Use Strict Equality
|
|
105
|
+
|
|
106
|
+
**BAD:** `==` converts types
|
|
107
|
+
```javascript
|
|
108
|
+
const testme = '1';
|
|
109
|
+
if (testme == 1) {
|
|
110
|
+
// Executes! '1' is converted to integer
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**GOOD:** `===` checks both type and value
|
|
115
|
+
```javascript
|
|
116
|
+
const testme = '1';
|
|
117
|
+
if (testme === 1) {
|
|
118
|
+
// Does NOT execute
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Efficient Loops
|
|
123
|
+
|
|
124
|
+
**BAD:** Checks array.length every iteration
|
|
125
|
+
```javascript
|
|
126
|
+
const names = ['Jeff', 'Nolan', 'Marshall', 'Don'];
|
|
127
|
+
for (let i = 0; i < names.length; i++) {
|
|
128
|
+
process(names[i]);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**GOOD:** Cache length
|
|
133
|
+
```javascript
|
|
134
|
+
const names = ['Jeff', 'Nolan', 'Marshall', 'Don'];
|
|
135
|
+
for (let i = 0, j = names.length; i < j; i++) {
|
|
136
|
+
process(names[i]);
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Even better:** ES6 `for...of`
|
|
141
|
+
```javascript
|
|
142
|
+
for (const name of names) {
|
|
143
|
+
process(name);
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Use var/let/const Correctly
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
// ES5: Use var (function-scoped)
|
|
151
|
+
var x = 10;
|
|
152
|
+
|
|
153
|
+
// ES6+: Use let and const (block-scoped)
|
|
154
|
+
let y = 20; // Can be reassigned
|
|
155
|
+
const z = 30; // Cannot be reassigned
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Common Patterns
|
|
161
|
+
|
|
162
|
+
### Ternary Operator
|
|
163
|
+
|
|
164
|
+
Compact conditional assignment:
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
// Instead of this:
|
|
168
|
+
let xyz;
|
|
169
|
+
if (somecondition === somevalue) {
|
|
170
|
+
xyz = 'abc';
|
|
171
|
+
} else {
|
|
172
|
+
xyz = '123';
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Use this:
|
|
176
|
+
const xyz = (somecondition === somevalue) ? 'abc' : '123';
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Multiple Variable Declarations
|
|
180
|
+
|
|
181
|
+
Use commas instead of multiple `var` statements:
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// Instead of this:
|
|
185
|
+
const foo = true;
|
|
186
|
+
const me = 'awesome';
|
|
187
|
+
const great = 42;
|
|
188
|
+
|
|
189
|
+
// Use this:
|
|
190
|
+
const foo = true,
|
|
191
|
+
me = 'awesome',
|
|
192
|
+
great = 42;
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Self-Calling Functions (IIFE)
|
|
196
|
+
|
|
197
|
+
Encapsulate private variables and functions:
|
|
198
|
+
|
|
199
|
+
**Ambiguous (not recommended):**
|
|
200
|
+
```javascript
|
|
201
|
+
const myValue = (() => {
|
|
202
|
+
return someValue;
|
|
203
|
+
})(); // Looks like assigning function, not result
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**Clear (recommended):**
|
|
207
|
+
```javascript
|
|
208
|
+
const myValue = (() => {
|
|
209
|
+
// Private variables here
|
|
210
|
+
const privateVar = 'secret';
|
|
211
|
+
|
|
212
|
+
return someValue;
|
|
213
|
+
})(); // Clearly returns result, not function
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### CommonJS Modules
|
|
217
|
+
|
|
218
|
+
Use `require()` for modular code:
|
|
219
|
+
|
|
220
|
+
**myModule.js:**
|
|
221
|
+
```javascript
|
|
222
|
+
// Private
|
|
223
|
+
const privateData = 'secret';
|
|
224
|
+
|
|
225
|
+
function privateHelper() {
|
|
226
|
+
// Private function
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Public API
|
|
230
|
+
exports.publicMethod = () => {
|
|
231
|
+
privateHelper();
|
|
232
|
+
return privateData;
|
|
233
|
+
};
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**app.js:**
|
|
237
|
+
```javascript
|
|
238
|
+
const myModule = require('myModule');
|
|
239
|
+
myModule.publicMethod();
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## ES6+ Features in Titanium
|
|
245
|
+
|
|
246
|
+
Titanium supports modern JavaScript (ES6/ES7+). Use these features:
|
|
247
|
+
|
|
248
|
+
**Arrow Functions:**
|
|
249
|
+
```javascript
|
|
250
|
+
// Old
|
|
251
|
+
const self = this;
|
|
252
|
+
someArray.forEach(function(item) {
|
|
253
|
+
self.process(item);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// New (arrow function preserves 'this')
|
|
257
|
+
someArray.forEach((item) => {
|
|
258
|
+
this.process(item);
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Template Literals:**
|
|
263
|
+
```javascript
|
|
264
|
+
// Old
|
|
265
|
+
const message = 'Hello ' + name + ', you have ' + count + ' messages';
|
|
266
|
+
|
|
267
|
+
// New
|
|
268
|
+
const message = `Hello ${name}, you have ${count} messages`;
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
**Destructuring:**
|
|
272
|
+
```javascript
|
|
273
|
+
// Old
|
|
274
|
+
const width = e.source.size.width;
|
|
275
|
+
const height = e.source.size.height;
|
|
276
|
+
|
|
277
|
+
// New
|
|
278
|
+
const {width, height} = e.source.size;
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
**Spread Operator:**
|
|
282
|
+
```javascript
|
|
283
|
+
// Old
|
|
284
|
+
const arr = [1, 2, 3];
|
|
285
|
+
const arr2 = arr.concat([4, 5]);
|
|
286
|
+
|
|
287
|
+
// New
|
|
288
|
+
var arr2 = [...arr, 4, 5];
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
**Classes:**
|
|
292
|
+
```javascript
|
|
293
|
+
class Person {
|
|
294
|
+
constructor(name) {
|
|
295
|
+
this.name = name;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
greet() {
|
|
299
|
+
return `Hello, I'm ${this.name}`;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
---
|
|
305
|
+
|
|
306
|
+
## Gotchas to Avoid
|
|
307
|
+
|
|
308
|
+
### Variable Hoisting
|
|
309
|
+
|
|
310
|
+
**Problem:** Variables are hoisted to function scope
|
|
311
|
+
```javascript
|
|
312
|
+
function test() {
|
|
313
|
+
console.log(myVar); // undefined (not error!)
|
|
314
|
+
var myVar = 'value';
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
**Solution:** Declare variables at top of function
|
|
319
|
+
```javascript
|
|
320
|
+
function test() {
|
|
321
|
+
let myVar; // Declare first
|
|
322
|
+
console.log(myVar);
|
|
323
|
+
myVar = 'value';
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Global Variable Accidents
|
|
328
|
+
|
|
329
|
+
**Problem:** Forgetting `var` creates global variable
|
|
330
|
+
```javascript
|
|
331
|
+
function test() {
|
|
332
|
+
myVar = 'value'; // Creates GLOBAL variable!
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
**Solution:** Always use `var`, `let`, or `const`
|
|
337
|
+
```javascript
|
|
338
|
+
function test() {
|
|
339
|
+
const myVar = 'value'; // Local variable
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### The `this` Context
|
|
344
|
+
|
|
345
|
+
**Problem:** `this` changes in different contexts
|
|
346
|
+
```javascript
|
|
347
|
+
const obj = {
|
|
348
|
+
value: 42,
|
|
349
|
+
getValue: function() {
|
|
350
|
+
setTimeout(function() {
|
|
351
|
+
console.log(this.value); // undefined!
|
|
352
|
+
}, 100);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
**Solution 1:** Store `this`
|
|
358
|
+
```javascript
|
|
359
|
+
const obj = {
|
|
360
|
+
value: 42,
|
|
361
|
+
getValue: function() {
|
|
362
|
+
const self = this;
|
|
363
|
+
setTimeout(function() {
|
|
364
|
+
console.log(self.value); // Works!
|
|
365
|
+
}, 100);
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
**Solution 2:** Arrow function (ES6)
|
|
371
|
+
```javascript
|
|
372
|
+
const obj = {
|
|
373
|
+
value: 42,
|
|
374
|
+
getValue() {
|
|
375
|
+
setTimeout(() => {
|
|
376
|
+
console.log(this.value); // Works!
|
|
377
|
+
}, 100);
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Semicolon Insertion
|
|
383
|
+
|
|
384
|
+
JavaScript automatically inserts semicolons, but can cause bugs:
|
|
385
|
+
|
|
386
|
+
**Dangerous:**
|
|
387
|
+
```javascript
|
|
388
|
+
return
|
|
389
|
+
{
|
|
390
|
+
name: 'Test'
|
|
391
|
+
};
|
|
392
|
+
// Returns undefined!
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
**Safe:**
|
|
396
|
+
```javascript
|
|
397
|
+
return {
|
|
398
|
+
name: 'Test'
|
|
399
|
+
};
|
|
400
|
+
// Or put opening brace on same line
|
|
401
|
+
return { name: 'Test' };
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
## Resources
|
|
407
|
+
|
|
408
|
+
- **Alloy Framework:** See `alloy-guides` skill
|
|
409
|
+
- **CommonJS Modules:** See `commonjs-advanced.md`
|
|
410
|
+
- **Coding Best Practices:** See `coding-best-practices.md`
|
|
411
|
+
- **Style Guide:** See `style-and-conventions.md`
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Reserved Words in Titanium
|
|
2
|
+
|
|
3
|
+
## ECMAScript Reserved Keywords
|
|
4
|
+
|
|
5
|
+
These keywords cannot be used as variable, function, method, or object identifiers per the ECMAScript specification:
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
abstract, boolean, break, byte, case, catch, char, class, const, continue,
|
|
9
|
+
debugger, default, delete, do, double, else, enum, export, extends, finally,
|
|
10
|
+
for, function, goto, if, implements, import, in, instanceof, int, interface,
|
|
11
|
+
let, long, native, new, package, private, protected, public, return, short,
|
|
12
|
+
static, super, switch, synchronized, this, throw, throws, transient, try,
|
|
13
|
+
typeof, var, void, volatile, while, with, yield
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## iOS Objective-C Reserved Words
|
|
17
|
+
|
|
18
|
+
These keywords are exposed from Objective-C and may not be used as identifiers on iOS:
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
_configure, _destroy, _initProperties, autorelease, deadlock, dealloc,
|
|
22
|
+
description, id, init, release, startup
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Alloy Reserved Words
|
|
26
|
+
|
|
27
|
+
**Do not use double underscore prefixes** on variables, properties, or function names (e.g., `__foo`). These are reserved for Alloy's internal use.
|
|
28
|
+
|
|
29
|
+
## Best Practice
|
|
30
|
+
|
|
31
|
+
Always avoid these reserved words when naming:
|
|
32
|
+
- Variables
|
|
33
|
+
- Functions
|
|
34
|
+
- Methods
|
|
35
|
+
- Object properties
|
|
36
|
+
- Module exports
|