diff --git a/source/Ox/js/Fallback.js b/source/Ox/js/Fallback.js index 489b3494..bc929b7f 100644 --- a/source/Ox/js/Fallback.js +++ b/source/Ox/js/Fallback.js @@ -2,6 +2,45 @@ Ox.fallback = {}; +(function(global) { + /*@ + Ox.fallback.bind = see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind + + > Ox.test.object.get() + 'foo' + > Ox.test.get() + 'bar' + > Ox.fallback.bind.call(Ox.test.get, Ox.test.object)() + 'foo' + @*/ + Ox.fallback.bind = function(that) { + if (typeof this !== 'function') { + throw new TypeError(); + } + var args = Array.prototype.slice.call(arguments, 1), + fn = function() {}, + this_ = this, + ret = function() { + return this_.apply( + this instanceof fn ? this : that || global, + args.concat(Array.prototype.slice.call(arguments)) + ); + }; + fn.prototype = this.prototype; + ret.prototype = new fn(); + return ret; + }; +})(this); + /*@ Ox.fallback.every see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every > Ox.fallback.every.call([0, 1, 2], function(v, i) { return v == i; }) @@ -225,13 +264,19 @@ Ox.fallback.trim = function() { (function() { var method, object; for (method in Ox.fallback) { - object = method == 'keys' ? Object + object = method == 'bind' ? Function.prototype + : method == 'keys' ? Object : method == 'trim' ? String.prototype : Array.prototype; - for (method in Ox.fallback[object]) { - if (!object[method]) { - object[method] = Ox.fallback[method]; - } + if (!object[method]) { + object[method] = Ox.fallback[method]; } } + // IE9 in developer mode has window.console, but no window.console.log.apply + // see http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function + if (window.console && !window.console.log.apply) { + window.console.log = Function.prototype.bind.call( + window.console.log, window.console + ); + } })();