7. i
Adding a double slash before the closing star-
slash allows you to comment and uncomment
the whole block by simply adding or removing a
slash before the opening slash-star.
/* */ instead of //
function(){
var me = this;
/*
function enable(x){
x.setDisabled(false);
COMMEN };
// */
};
T with
9. function(data){ !
var localVar = data; //Correct : defines a local variable If you declare a variable, without using "var",
globalVar = data; // declares a global variable the variable always becomes GLOBAL.
}; Forgetting to declare a variable is a very
common mistake. It creates bugs that can be
very difficult to find.
USE single var dayArray = [‘Mon’, ‘Tue’, ‘Wed’]; var dayArray = [‘Mon’, ‘Tue’, ‘Wed’],
var dayArrayLength = dayArray.length; dayArrayLength = dayArray.length,
‘var’ instead of var dayOne = dayArray[0]; dayOne = dayArray[0];
many
AVOID using global
variables
USE „var‟
10. if (true) if (true){
UNCLEAR
console.log("inside the if statement.");
console.log("outside the if statement.");
console.log("inside the if statement.");
} console.log("outside the if statement.");
var localVar = new Object(); var localVar = new Object{
name : ‘manav’;
url : ‘http://about.me/manavg’;
SLOW };
var localVar = new Array(); var localVar = [‘manav’,’gaurav’];
USE Braces {}
11. Statements that are effect by automatic semicolon insertion
empty
var
expression return
do-while ’something’;
continue // is transformed to
break return;
return ’something’;
throw
Automatic ; (semicolon)
12. Current
break for throw
case function try
catch if typeof
continue in var
default instanceof void
delete new while
do return with
else switch
finally this
For Future
abstract final protected
boolean float public
byte goto short
char implements static
class import super
const int synchronized
debugger interface throws
double long transient
enum native volatile
export package
extends private
13. switch (color) {
case ‘blue’:
if (color === ‘blue’) { // do something here...
// do something here... break;
} else if (color === ‘green’) { case ‘green’:
// do something here... // do something here...
} else if (color === ‘yellow’) { break;
// do something here... case ‘yellow’:
} else if (color === ‘red’) { // do something here...
// do something here... break;
} else { case ‘red’:
// do something here... // do something here...
} break;
default:
// do something here...
USE the “switch” to
}
Handle Multiple
Conditions
14.
15. DON‟T Change a
Variable‟s Type After
Initial Declaration
var value = ‚One Hundred";
value = 100;
Not all change is good
16. var human = {
var human = new Object();
name : ‘Manav’,
human.name = ‘Manav’;
gender : ‘male’,
human.gender = ‘male’;
say : function(){
human.say = function(){
return ‘I am’ + this.name;
return ‘I am’ + this.name;
}
};
};
var department = [
var department = new Array();
‘Engineering’,
department[0] = ‘Engineering’;
‘Operations’,
department[1] = ‘Operations’;
‘Management’
department[2] = ‘Management’;
];
var direction;
if(x > 0){
direction = ‘right’;
var direction = (x > 0) ? ‘right’ : ‘left’;
} else {
direction = ‘left’;
}
if(v){
var x = v;
} else { var x = v ? v : 10; var x = v || 10;
}
var x = 10;
USE Shortcut
17. Optimize
loops What could
be faster?
var names = ['Red','Blue','Green',‘Yellow'];
for(var i=0;i<names.length;i++){
meshColor (names[i], names[i-1]);
}
Better
var names = ['Red','Blue','Green',‘Yellow'];
var all = names.length;
for(var i=0;i<all;i++){
meshColor(names[i], names[i-1]);
}
var names = ['Red','Blue','Green',‘Yellow'];
for(var i=0,j=names.length;i<j;i++){
meshColor (names[i], names[i-1]);
}
18. function showUsers(users) {
/* verify that users is an non-empty array */
if(typeof users === ‚array‛ && users.length) {
/* process users array */
}
function callback(data) { }
/* verify that data is not null */
if(!data) {
throw new Error(‚CampaignService.getCampaignData returned null.‛);
return false;
}
/* process data */
}
function saveForm() {
var f = this.getForm();
if(!f.isValid()) {
alert(‚Fill in the form as required.‛);
return false;
}
/* save data */
}
DON‟T Trust External
Data
19. USE Object Literals
as
Configuration
Objects
Ext.create({
Object literals are objects xtype: ‚textfield‛,
name: ‛firstName‛,
defined using braces {} that label: ‚First Name‛,
contain a set of comma allowBlank: false,
readOnly: false,
separated key-value pairs much minLength: 10
});
like a map in Java.
20. Value Type
0 Number
NaN Number
‘’ String
false Boolean
null Object
undefined Undefined
The Falsy Values of
Javascript
21. USE ‘.’ & [] notations
wisely
var x = obj[‘x’]; var x = obj.x;
var x = obj[x];
[] notation provides
dot notation is faster
flexibility but is expensive
1. If a property is visible externally or is mixed with external properties than use
square brackets
2. If a property is internal only to the project then use dot
3. If you're using a lot of square brackets think about binding it to a function