JavaScript 赋值运算符
<p><span style="font-size: 16px">赋值运算符(assignment operator)是将右边操作数(right operator)的值赋值给左边的操作数(left operator)</span></p><p><span style="font-size: 16px">如果在等号( = )前面再添加乘性操作符、加性操作符或位操作符,就可以完成复合赋值操作。这种复合赋值操作相当于是对常规表达式的简写。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> num = 10<span style="color: rgba(0, 0, 0, 1)">;
num </span>= num +10<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> num = 10<span style="color: rgba(0, 0, 0, 1)">;
num </span>+= 10;</pre>
</div>
<p><span style="font-size: 16px">每个主要算数运算符(以及个别的其他操作符)都有对应的符合赋值操作符,这些操作符如下:</span></p>
<table style="background-color: rgba(243, 243, 243, 1)" border="0" cellpadding="4">
<tbody>
<tr>
<td><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">名称</span></strong></span></td>
<td><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">简写</span></strong></span></td>
<td><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">含义</span></strong></span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">赋值(Assignment)</span></strong></td>
<td><span style="font-size: 16px">a = b</span></td>
<td><span style="font-size: 16px">a = b</span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">加赋值(Addition assignment)</span></strong></td>
<td><span style="font-size: 16px">a += b</span></td>
<td><span style="font-size: 16px">a = a + b</span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">减赋值(Subtraction assignment)</span></strong></td>
<td><span style="font-size: 16px">a -= b</span></td>
<td><span style="font-size: 16px">a = a - b</span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">乘赋值(Multiplication assignment)</span></strong></td>
<td><span style="font-size: 16px">a *= b</span></td>
<td><span style="font-size: 16px">a = a * b</span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">除赋值(Division assigment)</span></strong></td>
<td><span style="font-size: 16px">a /= b</span></td>
<td><span style="font-size: 16px">a = a / b</span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">模赋值(Remainder assignment)</span></strong></td>
<td><span style="font-size: 16px">a %= b </span></td>
<td><span style="font-size: 16px">a = a % b</span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">指数赋值(Exponentiation assignment)</span></strong></td>
<td><span style="font-size: 16px">a **= b<br></span></td>
<td><span style="font-size: 16px">a = a ** b <br></span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">左移赋值(Left shift assignment)</span></strong></td>
<td><span style="font-size: 16px">a <<= b</span></td>
<td><span style="font-size: 16px">a = a << b <br></span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">右移赋值(Right shift assignment)</span></strong></td>
<td><span style="font-size: 16px">a >>= b<br></span></td>
<td><span style="font-size: 16px">a = a >> b <br></span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">无符号右移赋值(Unsigned right shift assignment)</span></strong></td>
<td><span style="font-size: 16px">a >>>= b<br></span></td>
<td><span style="font-size: 16px">a = a >>> b <br></span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">按位与赋值(Bitwise AND assignment)</span></strong></td>
<td><span style="font-size: 16px">a &= b</span></td>
<td><span style="font-size: 16px">a = a & b <br></span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">按位异或赋值(Bitwise XOR assignment)</span></strong></td>
<td><span style="font-size: 16px">a ^= b</span></td>
<td><span style="font-size: 16px">a = a ^ b <br></span></td>
</tr>
<tr>
<td><strong><span style="font-size: 16px; color: rgba(138, 156, 198, 1)">按位或赋值(Bitwise OR assignment)</span></strong></td>
<td><span style="font-size: 16px">a |= b</span></td>
<td><span style="font-size: 16px">a = a | b</span></td>
</tr>
</tbody>
</table>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">一、赋值</span></h2>
<p><span style="font-size: 16px">简单的赋值运算,就是将一个值赋给一个变量。</span></p>
<p><span style="font-size: 16px">如果想把一个值赋给多个变量,可以采用链式使用赋值运算符。</span></p>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">语法:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px">a = b</span></pre>
</div>
<p><span style="font-size: 16px"><strong><span style="color: rgba(0, 51, 102, 1)">实例:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px"><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 假设有以下变量</span>
a = 2<span style="color: rgba(0, 0, 0, 1)">;
b </span>= 9<span style="color: rgba(0, 0, 0, 1)">;
c </span>= 7<span style="color: rgba(0, 0, 0, 1)">;
a </span>=<span style="color: rgba(0, 0, 0, 1)"> b;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将 b的值赋给 a,所以a 的值为 9</span>
a = b =<span style="color: rgba(0, 0, 0, 1)"> c
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将 c 的值赋给 b,b的值是 7,再将 b的值赋给 a,所以 a、b、c的值都是 7</span></span></pre>
</div>
<p> </p>
<h2><span style="background-color: rgba(0, 128, 128, 1); color: rgba(255, 255, 255, 1)">二、加赋值</span></h2>
<p><span style="font-size: 16px">加赋值运算符把一个右值和一个变量相加,然后把相加的变量赋值给该变量。</span></p>
<p><span style="font-size: 16px">两个操作符的类型决定加赋值运算符的行为。算数相加或是字符串连接都有可能。</span></p>
<p><span style="font-size: 16px; color: rgba(0, 51, 102, 1)"><strong>语法</strong>:</span></p>
<div class="cnblogs_code">
<pre>a +=<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a + b</pre>
</div>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">实例:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义下列变量</span>
aa = 'aa'<span style="color: rgba(0, 0, 0, 1)">;
bb </span>= 8<span style="color: rgba(0, 0, 0, 1)">;
cc </span>= <span style="color: rgba(0, 0, 255, 1)">true</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加赋值运算遵循着算数加运算符的规则</span>
bb += 1
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 数字相加9;</span><span style="font-size: 16px">实例:</span>
<span style="color: rgba(0, 0, 0, 1)">
cc </span>+= 8
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">布尔值与数字相加9</span>
<span style="color: rgba(0, 0, 0, 1)">
aa </span>+= <span style="color: rgba(0, 0, 255, 1)">false</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 布尔值与字符串相加 'aafalse'</span>
<span style="color: rgba(0, 0, 0, 1)">
aa </span>+=<span style="color: rgba(0, 0, 0, 1)"> bb
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 布尔值与数字相加拼串 'aa8'</span></pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">三、减赋值</span></h2>
<p><span style="font-size: 16px">减赋值运算符使一个变量减去右值,然后把结果赋给变量。</span></p>
<p><strong><span style="font-size: 16px; color: rgba(0, 51, 102, 1)">语法:</span></strong></p>
<div class="cnblogs_code">
<pre>a -=<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a - b</pre>
</div>
<p><span style="color: rgba(0, 51, 102, 1); font-size: 16px"><strong>实例:</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义下面的变量</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> aa = 2</span>
<span style="color: rgba(0, 0, 0, 1)">
aa </span>-= 3
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">aa的值为 -1</span>
aa -= 'wo'
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回值为 NaN</span></pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">四、除赋值</span></h2>
<p><span style="font-size: 16px">除赋值运算符使一个变量除以右面的值,并且将结果赋值给该变量。</span></p>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">语法:</span></strong></span></p>
<div class="cnblogs_code">
<pre>a /= b
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a / b</pre>
</div>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">实例:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 假定已定义了下面的变量</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">aa= 5</span>
<span style="color: rgba(0, 0, 0, 1)">
aa </span>/= 2 // 2.5<span style="color: rgba(0, 0, 0, 1)">
aa </span>/= "foo" //<span style="color: rgba(0, 0, 0, 1)"> NaN
aa </span>/= 0 // Infinity</pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">五、模赋值</span></h2>
<p><span style="font-size: 16px">模赋值运算使一个变量除以右值,然后把余数赋值给该变量。</span></p>
<p><strong><span style="font-size: 16px; color: rgba(0, 51, 102, 1)">语法:</span></strong></p>
<div class="cnblogs_code">
<pre>a %=<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a % b</pre>
</div>
<p><span style="color: rgba(0, 51, 102, 1); font-size: 16px"><strong>实例:</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">假设一个变量</span>
a = 5<span style="color: rgba(0, 0, 0, 1)">
a </span>%= 2 <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
a %= 'foo' <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> NaN</span>
a %= 0 <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> NaN</span></pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">六、左移赋值</span></h2>
<p><span style="font-size: 16px">左移赋值运算符使变量向左移动指定位数的比特位,然后将结果赋给该变量。</span></p>
<p><strong><span style="font-size: 16px; color: rgba(0, 51, 102, 1)">语法:</span></strong></p>
<div class="cnblogs_code">
<pre>a <<=<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a << b</pre>
</div>
<p><strong><span style="font-size: 16px; color: rgba(0, 51, 102, 1)">实例:</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> aa = 5; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">(00000000000000000000000000000101)</span>
aa <<= 2; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 20 (00000000000000000000000000010100)</span></pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">七、右移赋值</span></h2>
<p><span style="font-size: 16px">右移赋值运算符使变量向右移动指定位数的比特位,然后将结果赋值给变量</span></p>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">语法:</span></strong></span></p>
<div class="cnblogs_code">
<pre>a >>>=<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a >>> b</pre>
</div>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">实例:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> bar = 5; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (00000000000000000000000000000101)</span>
bar >>= 2; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1 (00000000000000000000000000000001)</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> bar = -5; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (-00000000000000000000000000000101)</span>
bar >>= 2;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> -2 (-00000000000000000000000000000010)</span></pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">八、无符号右移赋值</span></h2>
<p><span style="font-size: 16px">无符号右移赋值运算符向右移动指定数量的比特位,然后把结果赋给变量</span></p>
<p><strong><span style="font-size: 16px; color: rgba(0, 51, 102, 1)">语法:</span></strong></p>
<div class="cnblogs_code">
<pre>a >>>=<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a >>> b</pre>
</div>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">实例:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> bar = 5; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (00000000000000000000000000000101)</span>
bar >>>= 2;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1 (00000000000000000000000000000001)</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> bar = -5; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (-00000000000000000000000000000101)</span>
bar >>>= 2; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1073741822 (00111111111111111111111111111110)</span></pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">九、按位与赋值</span></h2>
<p><span style="font-size: 16px">按位与赋值使用两个操作值的二进制进行表示,执行二进制异或运算,并且把结果赋值给变量。</span></p>
<p><span style="font-size: 16px; color: rgba(0, 51, 102, 1)"><strong> 语法:</strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">a ^=</span><span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a ^ b</pre>
</div>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">实例:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> bar = 5<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 5: 00000000000000000000000000000101</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 2: 00000000000000000000000000000010</span>
bar &= 2; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 0</span></pre>
</div>
<p> </p>
<h2><span style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 128, 128, 1)">十、按位或赋值</span></h2>
<p><span style="font-size: 16px">按位或运算符使用两个操作值的二进制进行表示,执行按位或运算,并且把结果赋给变量。</span></p>
<p><span style="color: rgba(0, 51, 102, 1)"><strong><span style="font-size: 16px">语法:</span></strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px">a |=<span style="color: rgba(0, 0, 0, 1)"> b
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等价于</span>
a = a | b</span></pre>
</div>
<p><strong><span style="font-size: 16px; color: rgba(0, 51, 102, 1)">实例:</span></strong></p>
<div class="cnblogs_code">
<pre><span style="font-size: 14px"><span style="color: rgba(0, 0, 255, 1)">var</span> bar = 5<span style="color: rgba(0, 0, 0, 1)">;
bar </span>|= 2; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 7</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 5: 00000000000000000000000000000101</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 2: 00000000000000000000000000000010</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> -----------------------------------</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 7: 00000000000000000000000000000111</span></span></pre>
</div>
<p> </p>
<p> </p>
<p><span style="font-size: 16px">参考资料:</span></p>
<p>https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators</p><br><br>
来源:https://www.cnblogs.com/nyw1983/p/12184599.html 顶一个!lz整理得很详细啊,收藏了~
刚好最近在复习JavaScript基础,看了这个感觉清晰多了。复合赋值运算符平时确实经常用到,特别是 += 这种,写代码的时候能省不少功夫。
不过有个小建议哈,关于按位与赋值那里,lz写的是:
a ^= b
这里应该是 &= 吧? ^= 是按位异或赋值,我记得按位与赋值是 a &= b。麻烦lz看一下咯~
另外还想问一下,位运算这些在平时开发中用得多吗?感觉好像不太常见的样子...
总之感谢lz的分享,支持一下!👍
頁:
[1]