te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Why does console.log add a space at the end of my sentence, when adding a value with the comma operator? - Stack Ov
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does console.log add a space at the end of my sentence, when adding a value with the comma operator? - Stack Ov

programmeradmin3浏览0评论

This question is almost not worth asking, but here I am.

Referencing the question above, here is the code and output

var port = 3000

console.log("Listening on port ", port)

Outputs "Listening on port 3000"

Notice the extra space thrown in there. Why does this occur?

This question is almost not worth asking, but here I am.

Referencing the question above, here is the code and output

var port = 3000

console.log("Listening on port ", port)

Outputs "Listening on port 3000"

Notice the extra space thrown in there. Why does this occur?

Share Improve this question asked Sep 4, 2014 at 6:21 Keith GroutKeith Grout 9091 gold badge11 silver badges32 bronze badges 3
  • 3 Remove the space from the string, console.log already does that for you. – elclanrs Commented Sep 4, 2014 at 6:22
  • 2 Because that's what console.log does. Might as well ask why it prints on the console instead of painting your bathroom blue. (Because the latter is not what it does). :) If you don't want the space, use + operator to stitch strings together. – Amadan Commented Sep 4, 2014 at 6:26
  • 1 @Amadan Lol fair enough. This is actually the answer I was looking for. I understand the difference between , and +, and I wasn't looking for how to correct it. Only why it does that. Thanks to other answerers though. – Keith Grout Commented Sep 4, 2014 at 6:33
Add a ment  | 

3 Answers 3

Reset to default 8

By popular demand, copied from ments:

Because that's what console.log does. Might as well ask why it prints on the console instead of painting your bathroom blue. (Because the latter is not what it does). :) If you don't want the space, use + operator to stitch strings together.

The reason is, likely, the fact that console.log, at least in graphical clients, has the ability to present objects and arrays inline. This makes the displayed result not really a string, but more of a table, with the space separating each cell from another. Each argument is, thus, presented separately.

Also, considering that console.log is used for debugging, console.log(i, j, a[i][j], a[i][j] / 2) showing 3724.712.35 is not really all that useful, when pared to 3 7 24.7 12.35. And writing console.annoying_log(i + ' ' + j + ' ' + a[i][j] + ' ' + a[i][j] / 2) is annoying. I had enough of that from Java, when I was younger.

From https://developer.mozilla/en-US/docs/Web/API/console.log the method log takes as arguments a list of Javascript objects

console.log(obj1 [, obj2, ..., objN);

You're right in the documentation it says that the objects are appended together but it doesn't say that the separator is a single space.

obj1 ... objN A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

Should be be doing

 console.log("Listening on port " + port)

As (I think) it might be treating the arguments as an array and thus join it together with a default delimiter being a space

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论