Last active
June 12, 2017 09:12
-
-
Save lynxerzhang/7b0f9108cd14c333b82390c179f804a9 to your computer and use it in GitHub Desktop.
console.log and console.warn's Polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@see https://www.paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/ | |
//inspired from paulirish's console.log function | |
!(function(win, funNameAry, titleAry, max){ | |
max = max || 100; | |
for(var i = 0, funName = ""; i < funNameAry.length, funName = funNameAry[i]; i ++){ | |
win[funName] = (function(i, n){ | |
return function(){ | |
this[n].history = this[n].history || []; | |
this[n].logMax = this[n].logMax || max; | |
this[n].history.push(arguments); | |
if(this[n].history.length > this[n].logMax){ | |
this[n].history = this[n].history.slice(-this[n].logMax); | |
} | |
if(win.console && typeof win.console[n] === "function"){ | |
this[n].logbind = this[n].logbind || this.console[n].bind(this.console, "[" + titleAry[i] + "]"); | |
this[n].logbind.apply(null, Array.prototype.slice.call(arguments)); | |
} | |
} | |
})(i, funName); | |
} | |
}(window, ["log", "warn"], ["trace", "warning"])); | |
//usage: | |
log("trace to console"); | |
warn("warning for something"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment