function() {
var a = b = 0;
}
// same as ...
function() {
var a = (b = 0);
}
function zeroFill(x, y) {
angular = x >>> 2;
return angular;
}
function lala() {
var koko = 1
, mama = 2
, kaka = 3
, obj = {
tsoutsou: loulou
}
, mpe = 'mpou';
// implementation [...]
}
for (var i = 0; i < anArray.length; i++) {
console.log(anArray[i]);
}
for (var i = 0, max = anArray.length; i < max; i++) {
console.log(anArray[i]);
}
var flag = 0;
if (flag === false) {
// I refuse to execute bro..
}
var add = new Function('a, b', 'return a + b');
add(10, 10) // returns 20
// Function Declaration
function add(a, b) {
return a + b;
}
// Function Expression
// a.k.a anonymous function
var add = function (a, b) {
return a + b;
};
// Named Function Expression
var add = function add(a, b) {
return a + b;
};
// BAD
var o = new Object();
var a = new Array();
var s = new String();
var n = new Number();
var b = new Boolean();
// GOOD
var o = {};
var a = [];
var s = "";
var n = 0;
var b = false;
function readHugeData(callback) {
fs.readFile(pathToFile, function (err, data) {
var bufferString = data.toString();
callback(bufferString);
});
}
// callback function
function logData(buffer) {
console.log(buffer);
}
readHugeData(logData);
// BAD
function addPerson(firstName, lastName, age, location, address) {
doShit();
}
addPerson('Thanos', 'Theodoridis', 30, 'Thessaloniki', 'Kapou 10');
// GOOD
var person = {
firstName: 'Thanos',
lastName: 'Theodoridis',
age: 30,
location: 'Thessaloniki',
address: 'Kapou 10'
};
function addPerson(person) {
doShit();
}
addPerson(person);
var counter = function() {
var count = 0;
return function() {
return ++count;
}
}
var increment = counter();
increment(); // returns 1
increment(); // returns 2
increment(); // returns 3
var add = function add(a, b) {
return a + b;
};
// partially apply this function with "a"
// set to 1
var partial = add.bind(null, 1);
// complete the function application by also
// providing "b"
partial(2); // returns 3
(function () {
console.log('Bootstrap');
}());
var doSomething = function(param) {
if(!doSomething.cache[param]){
var result = {};
// ... computation heavy operation
doSomething.cache[param] = result;
}
return doSomething.cache[param];
};
doSomething.cache = {};
function Pet() {
//private member
var type = 'dog';
// public getter
this.getType = function() {
return type;
};
};
var indefix = new Pet();
console.log(indefix.type); // undefined
console.log(indefix.getType()); // "dog"
function Pet() {
// private member (?)
var petDetails = {
type: 'dog',
age: 20,
color: 'black'
};
// public getter
this.getPetDetails = {
return petDetails;
};
}
var myObject = (function (){
// private properties
var name = 'Thanos';
// public getter
return {
getName: function() {
return name;
}
}
}());
function Pet(){
// private member
var type = 'dog';
// public function
this.getType = function() {
return type;
};
}
Pet.prototype = (function () {
// private member
var food = 'Royal Canine';
// public prototype member
return {
getFavFood: function() {
return food;
}
};
}());
var indefix = new Pet();
console.log(indefix.getType()); // privileged "owned" method
console.log(indefix.getFood()); // privileged prototype method
// constructor
var Pet = function() {};
// static method
Pet.isFriendly = function(){
return false;
};
// normal method
Pet.prototype.feed = function() {
this.hungry = false;
};
// call static method
Pet.isFriendly(); // false
var indefix = new Pet();
indefix.feed();
Pet.prototype.isFriendly = Pet.isFriendly;
pet.isFriendly(); // false
var Item = (function() {
//static variable
var itemId = 0;
// the new constructor implementation
var NewItem = function() {
++itemId;
};
NewItem.prototype.getItemId = function() {
return itemId;
};
return NewItem;
}());
var iPhone = new Item();
iPhone.getItemId(); // 1
var macbookPro = new Item();
macbookPro.getItemId(); // 2
var obj = {
value: 1,
increment: function() {
++this.value;
return this;
},
add: function(a) {
this.value += a;
return this;
},
log: function() {
console.log(this.value);
}
};
obj.increment().add(10).log(); // 12
// VS
obj.increment();
obj.add(10);
obj.log();