Permalink
Browse files

console.log: if not string, coerce into one

  • Loading branch information...
1 parent 5aadeae commit 6b430a95c9a8c965f32d641dced169e6cecd61d6 @ry ry committed Jun 29, 2010
Showing with 1 addition and 1 deletion.
  1. +1 −1 src/node.js
View
2 src/node.js
@@ -194,7 +194,7 @@ process.openStdin = function () {
function format (f) {
var i = 1;
var args = arguments;
- if (!(f instanceof String)) return f;
+ if (!(f instanceof String)) f = String(f);
@DmitryBaranovskiy
DmitryBaranovskiy Jun 30, 2010

Just writing
f = String(f);
wouldn’t hurt, because anyway "abc" instanceof String === false

@ry
ry Jun 30, 2010

yeah, I was foolishly thinking this would be faster for the string case.

@eligrey
eligrey Jun 30, 2010

f += "" would also work fine, and is a little shorter. I use it in my libraries to convert stuff to strings.

@tj
tj Jun 30, 2010

''+f

works too

@DmitryBaranovskiy
DmitryBaranovskiy Jun 30, 2010

f += "" is good, but I was taught once that it could be confusing for other developers, later.

@tj
tj Jun 30, 2010

personally I like String() better, feels more like a cast

@TooTallNate
TooTallNate Jun 30, 2010

I 2nd (or 3rd, whichever it is) the String()

return f.replace(/%([sdf])/g, function (x) {
switch (x) {
case '%s': return args[i++];

0 comments on commit 6b430a9

Please sign in to comment.