-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1278 from stucox/fnbind
Change `fnBind` to a normal lib function instead of a polyfill
- Loading branch information
Showing
3 changed files
with
44 additions
and
96 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 |
---|---|---|
@@ -1,48 +1,56 @@ | ||
define(['slice'], function( slice ) { | ||
// Not implemented as a polyfill, as that would change the environment, | ||
// which isn’t something Modernizr should do | ||
|
||
// Defer to native .bind if available | ||
if ('bind' in Function.prototype) { | ||
return function fnBind (fn, that) { | ||
return fn.bind(that); | ||
}; | ||
} | ||
|
||
// Adapted from ES5-shim https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js | ||
// es5.github.com/#x15.3.4.5 | ||
function fnBind (fn, that) { | ||
|
||
if (!Function.prototype.bind) { | ||
Function.prototype.bind = function bind(that) { | ||
var target = fn; | ||
|
||
var target = this; | ||
if (typeof target != 'function') { | ||
throw new TypeError(); | ||
} | ||
|
||
if (typeof target != 'function') { | ||
throw new TypeError(); | ||
} | ||
var args = slice.call(arguments, 2); | ||
var bound = function() { | ||
|
||
var args = slice.call(arguments, 1); | ||
var bound = function() { | ||
if (fn instanceof bound) { | ||
|
||
if (this instanceof bound) { | ||
var F = function(){}; | ||
F.prototype = target.prototype; | ||
var self = new F(); | ||
|
||
var F = function(){}; | ||
F.prototype = target.prototype; | ||
var self = new F(); | ||
var result = target.apply( | ||
self, | ||
args.concat(slice.call(arguments)) | ||
); | ||
if (Object(result) === result) { | ||
return result; | ||
} | ||
return self; | ||
|
||
var result = target.apply( | ||
self, | ||
args.concat(slice.call(arguments)) | ||
); | ||
if (Object(result) === result) { | ||
return result; | ||
} | ||
return self; | ||
} else { | ||
|
||
} else { | ||
return target.apply( | ||
that, | ||
args.concat(slice.call(arguments)) | ||
); | ||
|
||
return target.apply( | ||
that, | ||
args.concat(slice.call(arguments)) | ||
); | ||
} | ||
|
||
} | ||
}; | ||
|
||
}; | ||
return bound; | ||
|
||
return bound; | ||
}; | ||
} | ||
|
||
return Function.prototype.bind; | ||
return fnBind; | ||
}); |
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
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