node/tools/eslint/node_modules/function-bind/implementation.js
Vse Mozhet Byt e5ca046c0a
build, doc, tools: add eslint-plugin-markdown
* Install eslint-plugin-markdown@1.0.0-beta.7
* Add doc/.eslintrc.yaml
* Add `plugins: [markdown]` to the main .eslintrc.yaml
* .js files in doc folder added to .eslintignore
* Update Makefile, vcbuild.bat, and tools/jslint.js

Refs: https://github.com/nodejs/node/pull/12563
Refs: https://github.com/nodejs/node/pull/12640
Refs: https://github.com/nodejs/node/pull/14047

PR-URL: https://github.com/nodejs/node/pull/14067
Reviewed-By: James Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
2017-07-30 23:11:10 -05:00

48 lines
1.4 KiB
JavaScript

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;
};