Skip to content

Instantly share code, notes, and snippets.

@kmarsh
Created August 19, 2014 15:32
Show Gist options
  • Save kmarsh/6b872f159891bf6b2874 to your computer and use it in GitHub Desktop.
Save kmarsh/6b872f159891bf6b2874 to your computer and use it in GitHub Desktop.

Revisions

  1. kmarsh created this gist Aug 19, 2014.
    37 changes: 37 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    var isFunction = function(o) {
    return typeof o == 'function';
    };


    var bind,
    slice = [].slice,
    proto = Function.prototype,
    featureMap;

    featureMap = {
    'function-bind': 'bind'
    };

    function has(feature) {
    var prop = featureMap[feature];
    return isFunction(proto[prop]);
    }

    // check for missing features
    if (!has('function-bind')) {
    // adapted from Mozilla Developer Network example at
    // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
    bind = function bind(obj) {
    var args = slice.call(arguments, 1),
    self = this,
    nop = function() {
    },
    bound = function() {
    return self.apply(this instanceof nop ? this : (obj || {}), args.concat(slice.call(arguments)));
    };
    nop.prototype = this.prototype || {}; // Firefox cries sometimes if prototype is undefined
    bound.prototype = new nop();
    return bound;
    };
    proto.bind = bind;
    }