Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i++原理 #14

Open
yinsang opened this issue Sep 24, 2020 · 0 comments
Open

i++原理 #14

yinsang opened this issue Sep 24, 2020 · 0 comments

Comments

@yinsang
Copy link
Owner

yinsang commented Sep 24, 2020

一个同事问起为什么这段代码的返回是这样的?

代码块
var i = 0;
i = i++
console.log(i)
一些人说是0 ,一些人说是1.其中的原理又是什么呢?

究其原理,++是运算符,=是赋值符。先运算,后赋值。

所以代码相当于

代码块
var i = 0;
i = (i++)
console.log(i)
代码的+是先返回后自加,问题的关键是自加的语句执行在赋值后还是赋值前?

https://blog.csdn.net/zhangpeng759/article/details/8630198

自加和自减都有一个变量temp来过渡

所以代码相当于

代码块
var i = 0;
// ++ 的运算过程
var temp ;
temp = i
i = i+1
// return temp
i=temp
console.log(i)
所以i最后还是0就能解释的通了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant