Sha256: a69103ac6ed0ce642264ed6c2895fea8de0313dcf96a4c0b2c326ef847d5fbfd

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible ';
var slice = Array.prototype.slice;
var toStr = Object.prototype.toString;
var funcType = '[object Function]';

module.exports = function bind(that) {
    var target = this;
    if (typeof target !== 'function' || toStr.call(target) !== funcType) {
        throw new TypeError(ERROR_MESSAGE + target);
    }
    var args = slice.call(arguments, 1);

    var bound;
    var binder = function () {
        if (this instanceof bound) {
            var result = target.apply(
                this,
                args.concat(slice.call(arguments))
            );
            if (Object(result) === result) {
                return result;
            }
            return this;
        } else {
            return target.apply(
                that,
                args.concat(slice.call(arguments))
            );
        }
    };

    var boundLength = Math.max(0, target.length - args.length);
    var boundArgs = [];
    for (var i = 0; i < boundLength; i++) {
        boundArgs.push('$' + i);
    }

    bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this,arguments); }')(binder);

    if (target.prototype) {
        var Empty = function Empty() {};
        Empty.prototype = target.prototype;
        bound.prototype = new Empty();
        Empty.prototype = null;
    }

    return bound;
};

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
lanes-0.8.0 node_modules/function-bind/implementation.js
minimum_viable_product-0.0.11 test/dummy/node_modules/function-bind/implementation.js
brwy_rails-0.0.6 test/dummy/node_modules/function-bind/implementation.js
brwy_rails-0.0.5 test/dummy/node_modules/function-bind/implementation.js
brwy_rails-0.0.4 test/dummy/node_modules/function-bind/implementation.js
brwy_rails-0.0.3 test/dummy/node_modules/function-bind/implementation.js
brwy_rails-0.0.2 test/dummy/node_modules/function-bind/implementation.js
brwy_rails-0.0.1 test/dummy/node_modules/function-bind/implementation.js