- A few best practices
Here are a few of the features of the language that make it a favorite among its developers:
- Dynamic typing (which allows Duck typing)
- Functional programming
- Convenient object literal notation
- A small set of standard functions and syntactical elements - learn the whole language in a day!
Online and virtual courses
- Codecademy is a interactive scripting tutorial site with both free and paid courses
Online Books and Resources
A few best practices
Don't pollute the global scope!
var keyword, your variables will be treated as global variables.) A much better approach than global variables/functions is to enclose all your application's API functions and properties into a single variable (namespace). This will minimize the chances of your code colliding with other code or libraries you include in the context later.
!== instead of
Called by some the "Compare, damn it!" operator,
=== performs what a
== operator might in other languages. If the two operands are equal in both type and value,
=== will return true and
? is assigned if the conditional statement is
true. The value after the
: is assigned if the conditional statement is
Lots of variables? Use a comma
You don't need to put
var in front of every variable - you can use commas to replace:
In most situations, checking the length of an array during every iteration can be slow. Moreso when working with Titanium proxy objects (that represent some native structure). So rather than writing:
It is better to only get the length of the array only once, as in:
Wrap self-calling functions in parenthesis
While syntactically correct, someone reading this code (missing the () at the end of the function declaration) might think you are assigning a function to
myValue, rather than the return value of the function. A better way to write this is with wrapping parentheses:
In this case, it is more clear that
myValue is not a function, but the return value of the function.
Avoid deep nesting with Android/Rhino
On Android, Rhino will occasionally cause your app to run out of memory if you have several levels of recursion or iteration in a function. If you run into a "recursion too deep" error, try and flatten out your code in areas where you have several levels of nested loops or recursive code. If that's not a viable solution, you can also increase the size of the thread stack (see Android specific application properties section).
This issue is not a problem with the V8 engine on Android.