Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:27:15+00:00 2026-05-31T12:27:15+00:00

I’m learning more about javascript OOP by rolling my own console.log variant with some

  • 0

I’m learning more about javascript OOP by rolling my own console.log variant with some extra features I want.

So far I have

debug = {
        consoleAvailable :  (typeof console == "object"),
        reportToConsole : 0,
        list            : [],
        setReporting    : function(level){
                            this.reportToConsole = level;
                            return true;
                        },
        log             : function(){
                            if (this.reportToConsole>0 && this.consoleAvailable && typeof console.log=="function")  console.log.apply(console, this.log.arguments);
                            this.list.push({'type':'log', 'msg':this.log.arguments});
                            return true;
                        },
};

This is all working nicely, but I don’t want to have to list all of the log,error,warning etc functions. Instead I would like to be able to just type debug.[something] and for a function to interpret that [something] and execute it in the same way as I have the log function working.

Is this even possible? If so how do I go about it?

Here is some examples of what I would like to be able to do.

debug.setReporting(1); //yes, I want to print to console

debug.log('foo', 'bar', 'baz'); //arbitrary number of arguments (this is already working)
debug.error('qux'); //function I haven't named, but there is a console.error function
debug.arbitraryName([1, 2, 3]); //no function for console.arbitraryName (ideally it would just console.log the argument(s)

Edit

Ok, it looks like @Rob W’s method is the way to go, however I am having trouble implementing. It seems that I am not passing the name of the function correctly or similar. I have a fiddle here showing the problem http://jsfiddle.net/xiphiaz/mxF4m/

Conclusion

It looks like there is too many browser quirks to get a truly generic debugger without writing browser specific code, so instead I have just listed my most used log functions (log, warning and error). This does give me the option to further customize result of each of these functions.

result:

debug = {
    consoleAvailable :  (typeof console == "object"),
    reportToConsole : 0,
    list            : [],
    setReporting    : function(level){
                        this.reportToConsole = level;
                        return true;
                    },
    log             : function(){
                        if (this.reportToConsole>0 && this.consoleAvailable && typeof console.log=="function")  console.log.apply(console, this.log.arguments);
                        this.list.push({type:'log', msg:this.log.arguments});
                        return true;
                    },
    warn            : function(){
                        if (this.reportToConsole>0 && this.consoleAvailable && typeof console.warn=="function") console.warn.apply(console, this.warn.arguments);
                        this.list.push({type:'warn', msg:this.warn.arguments});
                        return true;
                    },
    error           : function(){
                        if (this.reportToConsole>0 && this.consoleAvailable && typeof console.error=="function")    console.error.apply(console, this.error.arguments);
                        this.list.push({type:'error', msg:this.error.arguments});
                        return true;
                    }
};

debug.setReporting(1);

debug.log('foo', 'bar', 'baz');
debug.error('qux');
debug.warn({test:"warning"});
console.log(debug.list);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:27:16+00:00Added an answer on May 31, 2026 at 12:27 pm

    You can get an array of all properties (including non-enumerable ones) using the Object.getOwnProprtyNames method. Then, enumerate through it, and check whether the console[key] is a function. If yes, extend your own object with the method.

    Object.getOwnPropertyNames(console)
    

    As for your last quesion, there’s a non-standard __noSuchMethod__ method which intercepts calls to undefined functions.

    I strongly recommend to use my first proposed method, because the console method will not magically grow bigger. It also makes debugging easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.