UPDATE: Here's my solution (inspired by accepted answer):
function log(msg, values) {
if(config.log == true){
msg = [msg];
var args = msg.concat(values);
console.log.apply( this, args );
}
}
UPDATE2: Even better solution:
function log(msg) {
if(config.log == true){
msg = [msg];
var values = Array.prototype.slice.call(arguments, 1);
var args = msg.concat(values);
console.log.apply( console, args );
}
}
You can call this version like so:
log("Hi my name is %s, and I like %s", "Dave", "Javascript");
Here's the original question:
console.log takes a string and replaces tokens with values, for example:
console.log("My name is %s, and I like %", 'Dave', 'Javascript')
would print:
My name is Dave, and I like Javascript
I'd like to wrap this inside a method like so:
function log(msg, values) {
if(config.log == true){
console.log(msg, values);
}
}
The 'values' arg might be a single value or several optional args. How can I accomplish this?
If I call it like so:
log("My name is %s, and I like %s", "Dave", "Javascript");
I get this (it doesn't recognize "Javascript" as a 3rd argument):
My name is Dave, and I like %s
If I call this:
log("My name is %s, and I like %s", ["Dave", "Javascript"]);
then it treats the second arg as an array (it doesn't expand to multiple args). What trick am I missing to get it to expand the optional args?