A small javascript library, fix console is undefined.
In ie6 ie7, console is undefined. In ie8 ie9, first open the developer tools before, console is undefined.
Fix ie6-9 console.xxx is error
console.log('xxx'); // Uncaught TypeError: Cannot read property 'log' of null
polyfill(); // fix
console.log('xxx'); // ok
Support fix api list:
- assert
- clear
- count
- debug
- dir
- dirxml
- exception
- error
- group
- groupCollapsed
- groupEnd
- info
- log
- profile
- profileEnd
- table
- time
- timeEnd
- timeStamp
- trace
- warn
The principle is overwrite console.xxx and add try catch, like below
console.log = function (...args) {
try {
console.log(...args);
} catch (e) {}
};
Like console.log, but is safe on ie6-9, is useful for library developers
The param is same with console.log
log('xxxx'); use log replace console.log
Like console.info, same as above
Like console.warn, same as above
Like console.error, same as above
safe exec console.xxx on ie 6-9, log
info
internal use of safeExec
safeExec('log', xxx); // console[log](xxx)
safeExec('assert', xxx); // console[assert](xxx)
only support one param, The difference between log and log1, log1 can remove by uglyjs, log can't remove by uglyjs
log internal implementation
function log(...args) {
try {
// can't remove by uglyjs
return apply.call(console[cmd], console, args);
} catch (e) {}
}
log1 internal implementation
function log1(msg) {
try {
// can remove by uglyjs
return console.log('log:', msg);
} catch (e) {}
}
demo
log1('xxxxx');
only support one param, like log1
only support one param, like log1