-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
[js] 第46天 写一个使两个整数进行交换的方法(不能使用临时变量) #175
Labels
js
JavaScript
Comments
利用运算符优先级和0* a = a + b;
b = a - b;
a = a - b; 异或取值 a ^= b;
b ^= a;
a ^= b; |
|
我去这种骚操作, 我只知道一个解构. |
var a = 1,b = 2;
a = b+a;
b = a-b;
a = a-b; |
ES 6 这个 优秀 |
不能使用临时变量的是什么意思求解答 |
1:a ^= b;
b ^= a;
a ^= b;
2:es6解构 [b,a]=[a,b]
在 2019年9月15日,下午3:13,LAO_SHEN <notifications@github.com<mailto:notifications@github.com>> 写道:
不能使用临时变量的是什么意思求解答
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#175?email_source=notifications&email_token=ABG6PUXYLHF4GJXAQSPTZ7LQJXOABA5CNFSM4HR54JIKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6XKY3Y#issuecomment-531541103>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ABG6PUUOD5FNV543LBYIXNDQJXOABANCNFSM4HR54JIA>.
|
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b); // => 2, 1 |
let a = 1, b = 2 |
let b = 1
let c = 2
let tmp
tmp = b
b = c
c = tmp
let a = 1;
let b = 2;
[a, b] = [b, a]; |
|
分享个新鲜的用法
|
es6 解构 |
ES6解构赋值 [a, b] = [b, a]; |
let x = 1;
let y = 2;
// 数组解构
[x, y] = [y, x]
// 运算符优先级
y = x + 0 * (x = y)
// 执行顺序
x = x + y
y = x - y
x = x - y
// 异或
x ^= y
y ^= x
x ^= y
console.log(x, y) // 2 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
第46天 写一个使两个整数进行交换的方法(不能使用临时变量)
The text was updated successfully, but these errors were encountered: