Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Learning JS is a bumpy road... I'd like to understand what happens inside my code, so console.log(); seems to be my friend.

From time to time I don't even know where errors came from (or I might be just too dumb) So I thought of better logging my app stack. I tried to find a answer for this simple yet complicated problem:

How to do that? Besides with console.log()

For example getting the name of a function (constructor) where the console.log is being called proved problematic:

function SomeFunction(argument1,argument2) {
    console.log(this+'> 01 message');
    console.log(this.name+'> 02 message');
    console.log(this.constructor+'> 03 message');
    console.log(this.constructor.name+'> 04 message');
    console.log(this.prototype+'> 05 message');
    console.log(this.constructor.method+'> 06 message');
}

SomeFunction.prototype.sayHello = function(} {
    console.log(this+'> 01 says Hello');
    console.log(this.name+'> 02 says Hello');
    console.log(this.constructor+'> 03 says Hello');
    // and so on... //
}

So. Which one is Correct? SomeFunction.constructor.name is working, but this syntax is quite long to use everytime, so something like

var fn = this.constructor.name makes sense, but thats just inefficient.

Can someone point me into the good practices direction, how do I squeeze the right log info from my code?

FYI: I searched into several books about this simple topic, and neither one says anything about it.

share|improve this question
2  
use breakpoints in the developer tools instead – Joseph the Dreamer Jul 9 '12 at 19:42
sorry but this is not the answer i look for – Patrick Jankun Jul 9 '12 at 19:45
I think you are left with the syntax (however verbose) the language and API provide you.. you can like you mention cast it to something else.. (fn) but why? these extra few characters will not break the bank for file size (and if they do then do what you originally suggest) – rlemon Jul 9 '12 at 19:47
1  
every quality browser I use provides a stack trace when errors are encountered. The only difficulty is when the error occurs in anonymous functions, which is solved by Joseph's suggestion. I think you just need to learn to love your debugger. – bkconrad Jul 9 '12 at 19:48
assume that I don't have a browser. I just like to know about any other methods that don't involve a browser and/or console.log ;) – Patrick Jankun Jul 9 '12 at 19:52
show 1 more comment

1 Answer

Use developer tools (F12, control-I) or get firebug and enjoy single-stepping thru your code, examining and altering variables, editing css. it will improve your javascript learning experience dramatically...

share|improve this answer
node on server, no browser there. – Patrick Jankun Jul 9 '12 at 19:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.