forked from Raynos/function-bind
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] return the native
bind
when available.
- Loading branch information
Showing
3 changed files
with
52 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,3 @@ | ||
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; | ||
}; | ||
var implementation = require('./implementation'); | ||
|
||
module.exports = Function.prototype.bind || implementation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters