Thursday, June 20, 2013

JavaScript function default parameter values


  
function f(a, b, c) {
    c = c || 'default value';
}
  

JavaScript Singleton


   
var oSingleton = new function() {
    /* Usual class implementation */
}
   

JavaScript XOR operator (in 3 characters)


    
if (A ?! B : B) {
    // A XOR B
}
     

O(n) JavaScript Unique Array


    
function arrayUnique(a) {
    var n = 0,
        o = {},
        a = [];
   
    for (;n<aRows.length;n++) { 
        if (!(aRows[n].id in o)) {
            o[aRows[n].id] = 1;
            a.push(aRows[n].id);
        }
    }
    return a;
}