add Function.prototype.bind and window.console.log.apply
This commit is contained in:
parent
67fcca7304
commit
2f5a99a88f
1 changed files with 50 additions and 5 deletions
|
@ -2,6 +2,45 @@
|
||||||
|
|
||||||
Ox.fallback = {};
|
Ox.fallback = {};
|
||||||
|
|
||||||
|
(function(global) {
|
||||||
|
/*@
|
||||||
|
Ox.fallback.bind = <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
|
||||||
|
<script>
|
||||||
|
Ox.test.object = {
|
||||||
|
get: function() {
|
||||||
|
return this.value;
|
||||||
|
},
|
||||||
|
value: 'foo'
|
||||||
|
};
|
||||||
|
Ox.test.get = Ox.test.object.get;
|
||||||
|
Ox.test.value = 'bar';
|
||||||
|
</script>
|
||||||
|
> 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 <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
|
Ox.fallback.every <f> 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; })
|
> Ox.fallback.every.call([0, 1, 2], function(v, i) { return v == i; })
|
||||||
|
@ -225,13 +264,19 @@ Ox.fallback.trim = function() {
|
||||||
(function() {
|
(function() {
|
||||||
var method, object;
|
var method, object;
|
||||||
for (method in Ox.fallback) {
|
for (method in Ox.fallback) {
|
||||||
object = method == 'keys' ? Object
|
object = method == 'bind' ? Function.prototype
|
||||||
|
: method == 'keys' ? Object
|
||||||
: method == 'trim' ? String.prototype
|
: method == 'trim' ? String.prototype
|
||||||
: Array.prototype;
|
: Array.prototype;
|
||||||
for (method in Ox.fallback[object]) {
|
if (!object[method]) {
|
||||||
if (!object[method]) {
|
object[method] = Ox.fallback[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
|
||||||
|
);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Add table
Reference in a new issue