1. JavaScript: prototype property

    In JavaScript, prototype is a property of functions which easily allows for object extension and inheritance.

    var x = {};
    x.prototype; // undefined
    
    x = [];
    x.prototype; // undefined
    
    x = function(){};
    x.prototype; // Object
    

    To extend an object with prototype:

    Array.prototype.add = function() {
      var length = this.length;
      var sum = 0;
      for (var i = 0; i < length; i++) {
        if (!isNaN(this[i])) {
          sum += this[i];
        }
      }
      return sum;
    };
    
    [2, 3, 5, 'ha ha ha ha', 10].add(); //20
    

    The above serves as an example. Do not extend native objects lightly. This topic is discussed extensively at javascriptweblog.

    To inherit from an object with prototype:

    var Parent = function() {
      this.name = 'parent';
      this.getName = function() {
        return this.name;
      };
    };
    
    var Child = function() {
      this.name = 'child';
    };
    
    Child.prototype = new Parent();
    Child.prototype.constructor = Child;
    
    var p = new Parent();
    p.getName(); // 'parent'
    
    var c = new Child();
    c.getName(); // 'child'
    
    Child.prototype.actChildish = function() {
      // childish behavior
    };
    
    c.actChildish();
    
    p.actChildish(); // error
    

    Note that updating Child’s prototype to include actChildish updates the already instantiated object c and that p did not get actChildish added to its prototype.

  2. JavaScript:

      jQuery === $;
      // true
    

  3. JavaScript:

      undefined = 10;
    
      typeof undefined; // 'number'
      undefined === 10 // true
    

  4. Regex:

    [ \t]+$
    

    Replace with “” to remove trailing whitespace from a file.

  5. JavaScript: test if a property is a function

      var isFunction = function(property) {
        return typeof property === 'function';
      }
    
      var x = {
        qq : 'QQ'
      };
    
      x.doStuff = function() {
        "stuff happened";
      };
    
      console.log(isFunction(x.doStuff)); // true
      console.log(isFunction(x.hasOwnProperty)); // true
      console.log(isFunction(x.qq)); // false
    

  6. JavaScript Type Coercion

    JavaScript:

      2 + "256" // "2256"
      2 - "256" // -254
    
      typeof (+"256") // number
      typeof (-"256") // number
    
      2 + +"256" // 258
      2 - -"256" // 258
    
      typeof -"" // number
      typeof +"" // number
    
      +"" === -"" // true
      +"" === 0 // true
      "" === 0 // false
    

  7. JavaScript: isInt function

      var isInt = function(x) {
        return x === parseInt(x, 10) && !isNaN(x);
      }
    
      console.log(isInt(755)); // true
      console.log(isInt(-644)); // true
      console.log(isInt(0)); // true
      console.log(isInt(3.14)); // false
      console.log(isInt("256")); // false
      console.log(isInt("-256")); // false
      console.log(isInt("7.62")); // false
      console.log(isInt("-0.14")); // false
      console.log(isInt(true)); // false
      console.log(isInt("false")); // false
      console.log(isInt("I'm singin' in the rain")); // false
      console.log(isInt({})); // false
      console.log(isInt([])); // false
      console.log(isInt(undefined)); // false
      console.log(isInt(null)); // false