Possible Duplicate:
Inspect the names/values of arguments in the definition/execution of a JavaScript function
When debugging javascript, I often have code like this:
function doSomething(a,b,c) {
console.log(a,b,c);
//function contents here
//...
}
This results in a line in the console like this:
0.0010719847172334315 0.002392010391772366 -2.764548758273147e-7
Which is hard to read. I want to have output like this:
a: 0.0010719847172334315, b: 0.002392010391772366, c: -2.764548758273147e-7
Is this possible to do? I don’t think it is possible to do in many languages. However, I don’t know javascript very well, and it seems like a language where it is possible to do clever things like this.
You can do something like this by taking in a parameter object and iterating over it:
Note that most of the cruft is due to you wanting to log the parameters on one line; in some other cases (such as logging one per line), you can simply call console.log on each of the individual strings and not bother with the array or the string join.