Nodejs Function Bind bind(oThis)

Here you can find the source of bind(oThis)

Method Source Code

Function.prototype.bind = Function.prototype.bind || function (oThis) {
  if( typeof this !== "function" ) {
    throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
  }//from   ww  w  .ja va2s.  c  om

  var aArgs = Array.prototype.slice.call(arguments, 1),
    fToBind = this,
    fNOP = function () {},
    fBound = function () {
      return fToBind.apply(
        (this instanceof fNOP && oThis ? this : oThis),
        aArgs.concat(Array.prototype.slice.call(arguments))
      );
    };

  fNOP.prototype = this.prototype;
  fBound.prototype = new fNOP();

  return fBound;
};

Related

  1. bindArg()
    Function.prototype.bindArg = function () {
        var args = arguments,
            that = this;
        return function () {
            that.apply(this, args);
        };
    };
    
  2. bind(a)
    Function.prototype.bind = function (a)
      if (arguments.length < 2 && typeof arguments[0] === 'undefined') return this;
      var b = this,
        c = Array.prototype.slice.call(arguments).slice(1);
      return function ()
        var d = c.concat(Array.prototype.slice.call(arguments));
        return b.apply(a, d)
    ...
    
  3. bind(scope)
    Function.prototype.bind = function(scope) {
      var _function = this;
      return function() {
        return _function.apply(scope, arguments);
    };
    String.prototype.trim = function () {
        return this.replace(/^\s*/, '').replace(/\s*$/, '');
    };
    ...
    
  4. bind(scope)
    Function.prototype.bind = function(scope) {
      var _function = this;
      return function() {
        return _function.apply(scope, arguments);
    
  5. bindbind(self)
    Function.prototype.bind = function bind(self) {
      var callable = this;
      return function binding() { return callable.apply(self, arguments.toArray()); };
    
  6. bind(scope)
    Function.prototype.bind = function(scope) {
        var func = this;
        return function() {
            return func.apply(scope, arguments);
        };
    };
    
  7. bind( obj )
    Function.prototype.bind = function( obj ) {
      var slice = [].slice,
        args = slice.call(arguments, 1), 
        self = this, 
        nop = function () {}, 
        bound = function () {
          return self.apply( this instanceof nop ? this : ( obj || {} ), 
                    args.concat( slice.call(arguments) ) );  
        };
    ...
    
  8. bind(bind)
    Function.prototype.bind = function (bind) {
        var self = this;
        return function () {
            var args = Array.prototype.slice.call(arguments);
            return self.apply(bind || null, args);
        };
    };
    
  9. rebind(n)
    Function.prototype.rebind = function (n) {
        var me = this;
        return function () {
            var res = me.apply();
            var f = res && res[n];
            if(typeof f !== 'function') throw 'routed property was not a function.';
            f.apply(null, arguments);
        };
    };
    ...