original
variable (function () { var global = this; var original = global.console; // var console = global.console = {}; })();
(function () { var global = this; var original = global.console; var console = global.console = {}; // var methods = ['assert', 'count', 'debug', 'dir', 'dirxml', 'error', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'trace', 'warn']; // for (var i = methods.length; i--;) { // , , // 'assert' (function (methodName) { // console[methodName] = function () { // if (original && methodName in original) { // original[methodName].apply(original, arguments); } }; })(methods[i]); } })();
// console.log $('#my-element').click(console.log); // var log = console.log; for (var i = 0; i < 10; i++) log(i);
Illegal invocation
". (function () { // .. var console = global.console = {}; console.production = false; // ... console[methodName] = function () { // if (original && methodName in original && !console.production) { // original[methodName].apply(original, arguments); // .. })();
console.production = true;
alert(typeof console.log);
object
no apply
method, so the easiest way to get around this bug is to call a method from a function prototype. A little bit of magic: original[methodName].apply(original, arguments); // : Function.prototype.apply.call(original[methodName], original, arguments);
var original = global.console; var console = global.console = {}; // => var original = global.console; delete global.console; var console = global.console = {};
// ... var original = global.console; var console = global.console = {}; if (original && !original.time) { original.time = function(name, reset){ if (!name) return; var time = new Date().getTime(); if (!console.timeCounters) console.timeCounters = {}; var key = "KEY" + name.toString(); if(!reset && console.timeCounters[key]) return; console.timeCounters[key] = time; }; original.timeEnd = function(name){ var time = new Date().getTime(); if(!console.timeCounters) return; var key = "KEY" + name.toString(); var timeCounter = console.timeCounters[key]; if (timeCounter) { var diff = time - timeCounter; var label = name + ": " + diff + "ms"; console.info(label); delete console.timeCounters[key]; } return diff; }; } // var methods = // ...
(function () { var global = this; var original = global.console; if ('console' in global) delete global.console; var console = global.console = {}; console.production = false; if (original && !original.time) { original.time = function(name, reset){ if (!name) return; var time = new Date().getTime(); if (!console.timeCounters) console.timeCounters = {}; var key = "KEY" + name.toString(); if(!reset && console.timeCounters[key]) return; console.timeCounters[key] = time; }; original.timeEnd = function(name){ var time = new Date().getTime(); if(!console.timeCounters) return; var key = "KEY" + name.toString(); var timeCounter = console.timeCounters[key]; if (timeCounter) { var diff = time - timeCounter; var label = name + ": " + diff + "ms"; console.info(label); delete console.timeCounters[key]; } return diff; }; } var methods = ['assert', 'count', 'debug', 'dir', 'dirxml', 'error', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'trace', 'warn']; for (var i = methods.length; i--;) { (function (methodName) { console[methodName] = function () { if (original && methodName in original && !console.production) { Function.prototype.apply.call(original[methodName], original, arguments); } }; })(methods[i]); } })();
Source: https://habr.com/ru/post/116852/
All Articles