|
1 | 1 | (function() { |
2 | 2 | /** |
3 | | - * Function.bind Polyfill for ECMAScript 5 Support |
4 | | - * Kangax's bind with Broofa's arg optimization. |
5 | | - * http://www.broofa.com/Tools/JSLitmus/tests/PrototypeBind.html |
| 3 | + * Function.bind for ECMAScript 5 Support |
6 | 4 | * |
7 | | - * Copied from https://gist.github.com/rxgx/1597825 |
| 5 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind |
8 | 6 | */ |
9 | | - if (typeof Function.prototype.bind !== "function") { |
10 | | - Function.prototype.bind = function() { |
11 | | - var slice = Array.prototype.slice; |
12 | | - return function(context) { |
13 | | - var fn = this, |
14 | | - args = slice.call(arguments, 1); |
15 | | - if (args.length) { |
16 | | - return function() { |
17 | | - return arguments.length |
18 | | - ? fn.apply(context, args.concat(slice.call(arguments))) |
19 | | - : fn.apply(context, args); |
| 7 | + |
| 8 | + if (!Function.prototype.bind) { |
| 9 | + Function.prototype.bind = function (oThis) { |
| 10 | + if (typeof this !== "function") { |
| 11 | + // closest thing possible to the ECMAScript 5 internal IsCallable function |
| 12 | + throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); |
| 13 | + } |
| 14 | + |
| 15 | + var aArgs = Array.prototype.slice.call(arguments, 1), |
| 16 | + fToBind = this, |
| 17 | + fNOP = function () {}, |
| 18 | + fBound = function () { |
| 19 | + return fToBind.apply(this instanceof fNOP && oThis |
| 20 | + ? this |
| 21 | + : oThis, |
| 22 | + aArgs.concat(Array.prototype.slice.call(arguments))); |
20 | 23 | }; |
21 | | - } |
22 | | - return function() { |
23 | | - return arguments.length |
24 | | - ? fn.apply(context, arguments) |
25 | | - : fn.call(context); |
26 | | - }; |
27 | | - }; |
| 24 | + |
| 25 | + fNOP.prototype = this.prototype; |
| 26 | + fBound.prototype = new fNOP(); |
| 27 | + |
| 28 | + return fBound; |
28 | 29 | }; |
29 | 30 | } |
30 | 31 | })(); |
31 | | - |
0 commit comments