3

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.

Inoperable
  • 1,429
  • 5
  • 17
  • 33
  • 2
    use breakpoints in the developer tools instead – Joseph Jul 09 '12 at 19:42
  • sorry but this is not the answer i look for – Inoperable Jul 09 '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 09 '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 09 '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 ;) – Inoperable Jul 09 '12 at 19:52
  • http://stackoverflow.com/questions/1911015/how-to-debug-node-js-applications – bkconrad Jul 10 '12 at 16:47

1 Answers1

0

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...

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84