Home >Web Front-end >JS Tutorial >JavaScript Advanced Programming (3rd Edition) Study Notes 4 js Operators and Operators_Basic Knowledge
在ECMAScript中,有非常丰富的运算符和操作符,在这篇文章中将按通常的分类来稍微整理一下,不过在整理之前,先说明一下:
1、虽然标题是运算符和操作符,然而在我看来并没有多少严格区分的必要,在英文中,貌似也是用一个Operator来表示,所以在下文中我可能会混用。甚至,一些不属于运算符和操作符范畴的,我也整理在这里,只要我觉得必要。
2、对于运算符的优先级,你无需一一牢记——我相信你知道最简单的”先乘除,后加减”,至于其它的,如果你不确定,加上括号好了。在ECMAScript中,优先级相同的从左向右运算。
3、对于一些编程语言通用的运算符,比如常用算术运算符(+-*/),我只会简单的列举一下,不会展开,但是请注意,并不是说这些不重要,相反,这些通用运算符甚至处于一个非常基础的地位,只是我觉得你应该早已经熟悉,没必要在这里花时间强调。
4、那么,这里重点关注什么呢?就是一些在ECMAScript中比较特殊的操作符,或者我认为值得花时间强调的一些地方。
运算符与操作符
类别 | 操作符 | 描述 | 说明 |
一元操作符 | ++ | 自增1 |
1、自增(减)有前置和后置两种类型,前置先自增(减)再参与其它运算,后置先参与其它运算再自增(减)。 2、在ES中,自增(减)不仅适用于整数,它们可以作用于任意值,对于不是Number类型的值,会先按前一篇文章中的规则隐式转换为Number,然后再自增(减),此时变量类型也会变成Number类型。 |
-- | 自减1 | ||
+ | 一元加 | 一元加最主要的应用就是将操作数转变为Number类型,相当于调用Number()转换。 | |
- | 一元减 | 一元减则是在一元加的基础之上再取其相反数。 | |
算术操作符 | + | 加 |
1、除了加(+)之外,如果操作数不是Number类型,会自动调用Number()转换为Number类型再进行计算。 2、对于加减(+-),除了作为算术运算符。还可以作为一元操作符(见上)。当然,由于字符串操作中对加号(+)的重载,还可以用于将任意数值(的字符串)相连,这也是第1点中为什么要除了加(+),它在含有非Number类型值时,会将所有操作数转换为字符串相连接。 3、与一般类C语言不同,在ES中,除(/)和取模(%)并不会区分整数和浮点数,比如 5 / 2 = 2.5 而不是2,5.3 % 3 = 2.3 而不是2。 4、任意运算,只要操作数含NaN,结果就是NaN。但并不是结果为NaN就一定有一个操作数为NaN,比如0/0也返回NaN。 5、对于含无穷Infinity的运算,规定比较多,这里就不列举了,可以参考原书,或者自行测试。 |
- | 减 | ||
* | 乘 | ||
/ | 除 | ||
% | 取模 | ||
逻辑操作符
(布尔操作符) |
! | 逻辑非 |
首先将操作数转换为Boolean类型值,然后再取反。可以使用双重非!!将一个数值转换为相应的Boolean值。 |
&& | 逻辑与 |
1、当两个操作数相应的Boolean值均为true时,返回true 2、短路:当第一个操作数相应的Boolean值为false时,会直接返回false,不会再计算第二个操作数。这常常被应用在判断一个变量(属性)是否有定义,如: |
|
|| | 逻辑或 |
1、当两个操作数相应的Boolean值至少有一个为true时,返回true 2、短路:当第一个操作数相应的Boolean值为true时,会直接返回true,不会再计算第二个操作数。 3、逻辑或,除了用于一般的判断之外,还常常被应用在提供默认值的情况,如: function Fn(obj){obj = obj || {}; } 这里如果调用Fn未传入obj,则会自动给obj赋值为undefined,然后因为undefined的相应Boolean值为false,所以会将一个空对象{}赋值给obj,如果调用传入了obj,则因为任意对象的Boolean值为true,所以就不会取后面的{},从而达到给obj一个默认值{}的效果。 这种方式还被应用在大型JS库的多个相对独立的文件中: //file2
|
|
关系操作符 (比较操作符) |
< | 小于 |
1、只要有一个操作数是Number类型或Boolean类型值,就将两个操作数转换成Number类型值(如果需要转换)执行数值比较。 2、字符串比较,会逐个比较字符编码值。 3、操作符是对象时,调用valueOf()(如果没有,就调用toString()),再将结果按上面规则比较。 4、任意数和NaN比较返回false。 |
<= | 小于或等于 | ||
> | 大于 | ||
>= | 大于或等于 | ||
== | 相等 |
1、相等和不等(==、!=)在比较时,只要有必要,就会隐式类型转换。 2、全等和不全等(===、!==)在比较时,不会转换类型,如果类型不一致,直接为!==。 3、结合1、2,可以知道,a===b则一定有a==b,而a!=b则一定有a!==b。 |
|
!= | No waiting | ||
=== | Congruent | ||
!== | Not congruent | ||
Assignment Operator | = | Assignment | |
Composite arithmetic assignment operator | Arithmetic operator plus = | corresponds to arithmetic operators, including =, -=, *=, /=, %= | |
Compound bitwise assignment operator | Bitwise operator plus = | corresponds to bitwise operators, including ~=, &=, |=, ^=, <<=, >>=, >>>= | |
Bitwise operators | ~ | Bitwise not | bitwise inversion, that is, return the complement code |
& | Bitwise AND | Bit-aligned, bit-by-bit operation, 1 will be returned only if both operation bits are 1, otherwise the bit will return 0, and finally the combination of all bit operation results will return | |
| | Bitwise OR | Bit-aligned, bit-by-bit operation, 0 will be returned only if both operation bits are 0, otherwise the bit will return 1, and finally the combination of all bit operation results will be returned | |
^ | Bitwise XOR | Bit-aligned, bit-by-bit operation, returns 1 when the two operation bits are different, otherwise the bit returns 0, and finally returns the combination of all bit operation results | |
<< | Move left | The binary number is shifted to the left, and the left shift will not change the sign bit | |
>> | Signed right shift | The binary number is shifted to the right, and the high bits are filled with matching bits | |
>>> | Unsigned right shift | The binary number is shifted to the right, directly shifted to the right. For positive numbers, the result is the same as >>. For negative numbers, the two's complement of the negative number will be treated as the binary code of the positive number | |
String operators | String concatenation | is equivalent to the concat() function, which will first convert all operands into strings and then concatenate them. Note that once a string is created, it will not change. When performing string concatenation, there will be an intermediate connection and destruction process in the background. This is also the reason why old browsers run slowly when a large number of string concatenation operations are performed. | |
= | String concatenation | a =b, equivalent to a=a b. | |
Object Operator | . | Attribute accessor | Simple object property accessor. |
[] | Property or (class) array access | Through [], you can access properties whose name is a variable or contains special characters. | |
new | Call the constructor to create the object | Returns a newly created object, and this inside the constructor is pointed to this newly created object. | |
delete | 变量、属性删除 | 删除属性(变量可以看成是全局对象或执行环境的一个属性)。 | |
void | 返回undefined。 | ||
in | 判断属性 | 对象属性或原型链上的属性。 | |
instanceof | 原型判断 | 比较同一个上下文中的对象是否为另一个对象的原型。 | |
其它操作符 | ?: | 条件操作符 | 语法;var value = exp ? trueExp : falseExp。 相当于var value; if(exp){ value = trueExp;}else{value = falseExp;} |
, | 逗号操作符 | 主要用于声明多个变量,这也是很多JS库的流行做法。例如:var num1=1,num2=2,num3=3; | |
() | 分组操作符 |
主要用途: 1、结合逗号操作符用于赋值。例如:var num = (5,1,4,8,0);这里num最后的值为0。 2、转换为表达式。比如eval('('+jsStr+')');又比如: |
|
typeof | 类型操作符 |
返回一个字符串值:Undefined类型—>'undefined'、Null类型—>'object'、Boolean类型—>'boolean'、Number类型—>‘number'、String—>'string'、内置Function对象的实例—>'function'、其它Object类型—>'object'。(有些浏览器实现略有不同) |
Explain a few points:
1. The classification here is not very strict. For example, bitwise NOT (~), logical NOT (!), delete, void, and typeof can all be regarded as unary operators, and autoincrement ( ) is also used in many materials. Are classified among arithmetic operators. When I was sorting it out, I mainly referred to the classification of the original book and also took into account naturalness.
2. The usage of the plus sign ( ) is relatively flexible. Please note that, especially when used in calculations, it is easy to make mistakes if you ignore the strings in it.
3. Typeof is generally used to determine simple data types. If it is an object type, because most of the returned objects are objects, it is of little practical use. The judgment of instanceof also needs to meet the conditions of the same context, otherwise an error will occur. , another more reliable method will be explained in detail when describing the object later.
4. First look at the following code: