Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create my own function with the same name as an existing to customize it?

Tags:

javascript

I have some console.log commands spread through my site.

Is it possible to override console.log with my own function? I want to customize the function so that it only logs if a specific variable is set to true.

In the end, I would still need to call the real console.log from this function.

Thanks Kevin

like image 918
Kevin Avatar asked Oct 07 '22 10:10

Kevin


1 Answers

Just create a closure, and store the original console.log function in a local variable.
Then override console.log, and call the original function after you do your check:

(function(){
    var original = console.log;

    console.log = function(){
        if ( log ) { // <-- some condition
            original.apply(this, arguments);
        }
    };
})();

Here's the fiddle: http://jsfiddle.net/J46w8/

like image 83
Joseph Silber Avatar answered Oct 13 '22 12:10

Joseph Silber