Ad
  • Custom User Avatar

    In most cases you don't want to do that. If function relay on this in its body (which is equal to window unless function defined in object or "use strict"; is used) you probably will encounter some side effects :) It's safer to pass null and use bind if you need to keep function's current context.

    Perhaps an example will make it clear:

    var obj = {
      val: 0,
      fn: function func() {
        this.val++;
        return this.val;
      }
    }
    window.val = 100;
    obj.fn.val = 1000;
    
    obj.fn(); // 1 -> this === obj
    obj.fn.apply(obj); // 2
    obj.fn.apply(null); // 101
    obj.fn.apply(obj.fn); // 1001
    
    var fn = obj.fn;
    // this now refer to window instance
    fn(); // 102
    fn.apply(null); // 103 -> same as fn()
    fn.apply(fn); // 1002 -> don't expected behaviour
    fn.apply(obj); // 3
    
    var bindedFn = obj.fn.bind(obj);
    // this now is always refer to obj
    bindedFn.apply(obj); // 4
    bindedFn.apply(null); // 5
    bindedFn.apply(obj.fn); // 6
    
  • Custom User Avatar

    [].slice will create new object and Array.prototype.slice don't

  • Custom User Avatar

    if (-1 !== handler) will always run code

  • Custom User Avatar

    propKey goes to global