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)
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745627564a4636902.html
评论列表(0条)