Bits on Bits on Bits

Javascript

Jquery Add

[Documentation] Combine multiple selectors together into one big selection:

1
$('div').add('p').css('background-color', 'green')

Counting Keys in an Object

1
2
elms = { a: 1, b: 2, c: 3 }
Object.keys(elms).length

Optional Params

From this StackOverflow answer:

1
optionalArg = (typeof optionalArg === "undefined") ? "defaultValue" : optionalArg;

Mocking the userAgent String

(useful for testing) From this StackOverflow answer:

1
2
3
navigator.__defineGetter__('userAgent', function(){
    return 'foo' // customized user agent
});

Watchpoints Based on an Object Property

From this SO answer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
console = console || {}; // just in case
console.watch = function(oObj, sProp) {
  sPrivateProp = "$_"+sProp+"_$"; // to minimize the name clash risk
  oObj[sPrivateProp] = oObj[sProp];

  // overwrite with accessor
  Object.defineProperty(oObj, sProp, {
    get: function () {
      return oObj[sPrivateProp];
    },

    set: function (value) {
      //console.log("setting " + sProp + " to " + value); 
      debugger; // sets breakpoint
      oObj[sPrivateProp] = value;
    }
  });
}