![](https://imageproxy.pixnet.cc/imgproxy?url=https://drbanana.ml/img/68747470733a2f2f6d6d62697a2e717069632e636e2f6d6d62697a5f6a70672f65584353526a794e5963624858446d484472567a333077376150356c326d4533556362533173686a69623459636458576461336b777a57564f7256634f786774667863374f31476f70313945724a704473696143313338772f3634303f77785f666d743d6a706567.webp)
英文 | https://javascript.plainenglish.io/as-a-software-engineer-7-ways-to-swap-the-value-of-variables-you-should-know-about-d9b4ad5afe23
你知道交換兩個變量的值有多少種方法?我很慚愧,因為我只知道兩種方法。事實上,至少有 7 種方法可以做到這一點,是不是有點太不可思議了?
這是每個前端開發工程師都應該知道的一種方法,我們可能在學生時代就學過這種技術。let a = 1let b = 2// Use a temporary variable to store the value of b firstlet temp = bb = aa = temptemp = nullconsole.log('a', a, 'b', b)這種方法的優點是非常簡單易懂。缺點是需要多聲明一個變量,這意味着程序需要占用更多的計算機內存。
2.使用ES6中的解構賦值
我相信大家對 ES6 已經很熟悉了,它有一個很棒的特性叫做解構。
使用解構功能,我們可以輕鬆交換兩個變量的值。
let a = 1let b = 2;[ a, b ] = [ b, a ]console.log('a', a, 'b', b)我個人真的很喜歡這種方式了,我們不需要使用任何額外的代碼來完成這項工作。
3. 使用異或 (^)
如果你對它非常感興趣,可以點擊這裡(鏈接)進行深入研究。
不過別着急,相信看完這張表的內容你就知道是怎麼回事了:
![](https://imageproxy.pixnet.cc/imgproxy?url=https://drbanana.ml/img/68747470733a2f2f6d6d62697a2e717069632e636e2f6d6d62697a5f706e672f65584353526a794e59635a7a4e474c4c4c6b43757376514138784d5173446f726963446d366f45315850356b4a39597a394c34686276696269627557304271386c4f4c796432514346696153364c6769616e34484d6e69634d436f412f3634303f77785f666d743d706e67.webp)
即使我們只需要知道這兩個知識點就足夠了:
(「0」^任何值)等於這個值。
100 ^ 100 等於 0
0 ^ 100 // 100100 ^ 100 // 0我想,你一定已經猜到該怎麼做了!
let a = 1let b = 2a = a ^ b// 1. => a ^ b ^ b// 2. => a ^ 0// 3. => b = a = 1b = a ^ b// 1. a ^ b => a ^ b ^ a// 2. a ^ b ^ a => 0 ^ b// 3. a = b = > 2a = a ^ bconsole.log('a', a, 'b', b)4. 使用加法交換兩個變量的值
什麼?你在搞笑嗎?沒有,你可以使用加法交換兩個變量的值。
let a = 1let b = 2a = a + b // The value of a is a(1) + b(2) = 3b = a - b // The value of b is a(3) - b(2) = 1a = a - b // The value of a is a(3) - b(1) = 2console.log('a', a, 'b', b)5. 使用減法交換兩個變量的值
既然我們可以使用加法交換兩個變量的值,為什麼不試試減法呢?
let a = 1let b = 2a = a - b // The value of a is a(1) - b(2) = -1b = b + a // The value of b is b(2) + a(-1) = 1a = b - a // The value of a is b(1) - a(-1) = 2console.log('a', a, 'b', b)那麼使用加法和減法到底有什麼區別呢?朋友們,你們一定知道JavaScript中有一個最大的安全數,那麼,如果我們使用加法,會不會超過這個數呢?是的,這個問題可以通過使用減法來避免。
6. 使用對象交換兩個變量的值
我們可以將對象用作交換兩個變量的橋樑,並讓它鏈接所有這些。
let a = 1let b = 2a = { a: a, b: b}b = a.aa = a.bconsole.log('a', a, 'b', b)你能用數組來完成這個嗎?
let a = 1let b = 2a = [ a, b ]b = a[ 0 ]a = a[ 1 ]console.log('a', a, 'b', b)7. 使用「,」逗號運算符
1).數組和「,」的組合:
let a = 1let b = 2a = [b, (b = a)][0]// The code roughly goes through the following steps// 1. b = a => b = 1// 2. a = [ 2, 1 ]// 2. a = [ 2, 1 ][0] => 2console.log('a', a, 'b', b)2).賓語與「,」的組合:
let a = 1let b = 2a = b + ((b = a), 0)// The code roughly goes through the following steps// 1. a = 2 + ((b = 1), 0) => b = 1// 2. a = 2 + 0 => a = 2console.log('a', a, 'b', b)總結
以上就是我今天跟分享的全部內容,如果你覺得有用的話,請記得點讚我,關注我,並將這篇文章分享給你的身邊的朋友,也許能夠幫助到他。
最後,感謝你的閱讀,祝編程愉快。
學習更多技能
請點擊下方公眾號
![](https://imageproxy.pixnet.cc/imgproxy?url=https://drbanana.ml/img/68747470733a2f2f6d6d62697a2e717069632e636e2f6d6d62697a2f7779696365386b46516866375772596a796e73444d6a52626743776961636b3445396e716f455262436d4469627352684570557135386f4b7749457963345675785647524f5153514c376c584972536f327972336e6f3149412f3634303f77785f666d743d676966.webp)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://drbanana.ml/img/68747470733a2f2f6d6d62697a2e717069632e636e2f6d6d62697a5f6a70672f65584353526a794e59635a696348583247656961445837733664696364554a7677753569624c3972544e58696166626d6d494c436b686770437249474544675a666c3078535171444f76476e6963334141423150633559765a6b6e412f3634303f77785f666d743d6a706567.webp)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://drbanana.ml/img/68747470733a2f2f6d6d62697a2e717069632e636e2f6d6d62697a5f706e672f65584353526a794e59635a6457584d6e415073634842737337614a784874375371646962657532556e63434a43655056716a71724d6c684b4c4a37397768676a52554d754a7a69616a614b757a575a66776963546d505573672f3634303f77785f666d743d706e67.webp)