Skip to content

Commit

Permalink
Further fixing for Modernizr .bind support (#21)
Browse files Browse the repository at this point in the history
This now catches bind errors for strange host implementations, and falls back to function wrapping there too
  • Loading branch information
Tim Perry committed Sep 25, 2013
1 parent c3936a9 commit 701fe87
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/loglevel.js
Expand Up @@ -7,7 +7,7 @@

;(function (undefined) {
var undefinedType = "undefined";

(function (name, definition) {
if (typeof module !== 'undefined') {
module.exports = definition();
Expand Down Expand Up @@ -38,17 +38,26 @@
var method = console[methodName];
if (method.bind === undefined) {
if (Function.prototype.bind === undefined) {
return function() {
Function.prototype.apply.apply(method, [console, arguments]);
};
return functionBindingWrapper(method, console);
} else {
return Function.prototype.bind.call(console[methodName], console);
try {
return Function.prototype.bind.call(console[methodName], console);
} catch (e) {
// In IE8 + Modernizr, the bind shim will reject the above, so we fall back to wrapping
return functionBindingWrapper(method, console);
}
}
} else {
return console[methodName].bind(console);
}
}

function functionBindingWrapper(f, context) {
return function() {
Function.prototype.apply.apply(f, [context, arguments]);
};
}

var logMethods = [
"trace",
"debug",
Expand Down

0 comments on commit 701fe87

Please sign in to comment.