admin管理员组文章数量:1434922
Here's the code..
function getMilli(){
return new Date().getTime().toString().substr(7,6);
}
console.log = function(p1, p2, p3, p4, p5, p6){
console.log(getMilli(), p1, p2, p3, p4, p5, p6);
}
It somehow does a "stack overflow" error but I don't get it why here.. I'm not recursively iterating things (at least I think)
...uh, yeah I was doing recursive stuff... but I didn't know how to do it. Thanks for the answers and great concepts.
Here's the code..
function getMilli(){
return new Date().getTime().toString().substr(7,6);
}
console.log = function(p1, p2, p3, p4, p5, p6){
console.log(getMilli(), p1, p2, p3, p4, p5, p6);
}
It somehow does a "stack overflow" error but I don't get it why here.. I'm not recursively iterating things (at least I think)
...uh, yeah I was doing recursive stuff... but I didn't know how to do it. Thanks for the answers and great concepts.
Share Improve this question edited Feb 6, 2012 at 19:27 JC JC asked Feb 6, 2012 at 18:10 JC JCJC JC 12.2k11 gold badges44 silver badges63 bronze badges 5- Looks like infinite recursion too me ! You're calling console.log from console.log ever and ever. – Benoit Blanchon Commented Feb 6, 2012 at 18:12
- because you are calling console.log from within console.log? – keymone Commented Feb 6, 2012 at 18:13
- 1 What you have here is log-ception... A log in a log in a log in a log... – Richard J. Ross III Commented Feb 6, 2012 at 18:15
- Avoid using p1, p2, p3 etc. if you can. – Ateş Göral Commented Feb 6, 2012 at 18:50
- @AtesGoral: thanks, yeah I just did it for quick demo :) – JC JC Commented Feb 6, 2012 at 19:26
3 Answers
Reset to default 4As an improvement to other answers that work:
- Avoid polluting the global scope or the properties of any object to store the original console.log.
- Avoid having to specify an arbitrary number of placeholder arguments like p1, p2, p3, etc.
Use a closure to store the original console.log
and also remove the dependency on arbitrary argument declarations:
console.log = function (log) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(getMillis()); // Push millis as first argument
log.apply(console, args);
};
}(console.log);
http://jsfiddle/9BPuc/
You are recursively calling the function console.log
. In other words, you're calling console.log
within console.log
.
What you probably meant to do is:
(function(){
var clog = console.log.bind(console);
console.log = function(p1, p2, p3, p4, p5, p6){
clog(getMilli(), p1, p2, p3, p4, p5, p6);
}
})();
apparently you are calling console.log
from within console.log
because when you reassign it to new function it does not retain link to the old one(and why should it?)
what you want to do instead is this:
console.old_log = console.log;
console.log = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(getMilli());
console.old_log.apply(console, args)
}
版权声明:本文标题:javascript - Why do I get "Maximum Call Stack Size Exceeded" when I override console.log? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745627563a2667067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论