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; } ?>What does x++ in JavaScript do? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What does x++ in JavaScript do? - Stack Overflow

programmeradmin5浏览0评论

I have a very short question, but it really confused me.

var y = 3, x = y++;

What is the value of x?

I thought the answer should be 4, but actually it's 3.

Can anyone explain me the reason?

I have a very short question, but it really confused me.

var y = 3, x = y++;

What is the value of x?

I thought the answer should be 4, but actually it's 3.

Can anyone explain me the reason?

Share Improve this question edited Sep 25, 2014 at 5:44 Barmar 782k56 gold badges546 silver badges660 bronze badges asked Sep 25, 2014 at 5:42 Yunhan LiYunhan Li 1531 gold badge3 silver badges8 bronze badges 2
  • 1 x = y++ here the value is assigned and then incremented. this x=++y will increment and assign. – Prabhu Murthy Commented Sep 25, 2014 at 5:44
  • when you do x = y++ ; first it will assign the value of y to x and then it increments. – K.D Commented Sep 25, 2014 at 5:45
Add a ment  | 

4 Answers 4

Reset to default 11

y++ is called post-increment -- it increments the variable after it returns the original value as the value of the expression. So

x = y++;

is equivalent to:

temp = y;
y = y + 1;
x = temp;

If you want to return the new value, you should use ++y. This is called pre-increment because it increments the variable before returning it. The statement

x = ++y;

is equivalent to:

y = y + 1;
x = y;

In y++, ++ is called "post-increment operator". It first uses the value of y, then increments y. In this, is contrasts to a "pre-increment operator", ++y, which first increments y, then returns the incremented value.

The ++ operator does two things. It increments a variable and it returns the value of the variable.

If you prefix it, ie ++y, it will return the value after the increment (in your case, 4). If you postfix (y++)it, it will return the value before the increment (in your case 3. Note that the value of y is now still 4, but x was assigned before y was incremented).

var y = 3;
x = ++y;
console.log(x);

This will result in 4

This is called "pre-increment". Where it returns the incremented value .

y++ is post-increment, which returns the original value.

发布评论

评论列表(0)

  1. 暂无评论