time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

_createBind.js (853B)


      1 var createCtor = require('./_createCtor'),
      2     root = require('./_root');
      3 
      4 /** Used to compose bitmasks for function metadata. */
      5 var WRAP_BIND_FLAG = 1;
      6 
      7 /**
      8  * Creates a function that wraps `func` to invoke it with the optional `this`
      9  * binding of `thisArg`.
     10  *
     11  * @private
     12  * @param {Function} func The function to wrap.
     13  * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
     14  * @param {*} [thisArg] The `this` binding of `func`.
     15  * @returns {Function} Returns the new wrapped function.
     16  */
     17 function createBind(func, bitmask, thisArg) {
     18   var isBind = bitmask & WRAP_BIND_FLAG,
     19       Ctor = createCtor(func);
     20 
     21   function wrapper() {
     22     var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
     23     return fn.apply(isBind ? thisArg : this, arguments);
     24   }
     25   return wrapper;
     26 }
     27 
     28 module.exports = createBind;