You are passing multiple arguments into your custom console.log
function, but you are only calling alert
on the first argument. To fix this, iterate over the arguments to your custom console.log
function, and call alert on each of the arguments:
var obj={result:true,type:"text"};
(function(){
var oldLog = console.log;
console.log = function () {
for (var i = 0; i < arguments.length; i++) {
alert(arguments[i]);
}
oldLog.apply(console, arguments);
};
})();
console.log("hi",obj);
If you want to have only one alert that displays all the objects, then you can concatenate their string representations in the for loop and call alert
only once at the end:
var obj={result:true,type:"text"};
(function(){
var oldLog = console.log;
console.log = function () {
var result = "";
for (var i = 0; i < arguments.length; i++) {
result += arguments[i] + " ";
}
alert(result);
oldLog.apply(console, arguments);
};
})();
console.log("hi",obj);
And finally, if you want to see the contents of the object in your alert, you can call JSON.stringify()
on each argument before it gets processed.