Javascript:Operators
来源:互联网 发布:淘宝详情页文案 编辑:程序博客网 时间:2024/06/11 03:42
In the previous article I talked about types and type coercion in JavaScript. In this one I want to talk more about how this coercion applies to JavaScript operators. Lets go over six major operators and look at how they work:
typeof
The typeof
operator returns a string representation of the type of the passed expression. There are two major points to note:
- Unresolvable references will produce
"undefined"
, i.e.typeof a
will return"undefined"
if variablea
was not declared. typeof
lies in two cases fornull
and forfunction () {}
.
Apart from this, operator works pretty much as a lookup table:
"undefined"
Null"object"
⚠Boolean"boolean"
Number"number"
String"string"
Object, that can’t be invoked"object"
Object, that can be invoked"function"
⚠I marked with “⚠” two places where operator is misleading: type of null
is Null and the actual type of any function is Object.
Subtraction
Converts both arguments to number. "8" - true
is converted to 8 - 1
. Very simple, indeed. Don’t expect the same from addition
Addition
Addition is one of the trickiest operators in JavaScript. Lets see what is going on when you write a + b
:
- Both arguments are converted to primitives. Lets call them
A
andB
. - If any of primitives is a String, concatenate
A
andB
as strings. - Otherwise add
A
andB
as numbers.
For example:
1
8 +
"5"
➙
"8"
+
"5"
➙
"85"
;
2
8 +
true
➙ 8 + 1 ➙ 9;
3
"8"
+
true
➙
"8"
+
"true"
➙
"8true"
;
4
Less-than
In contrast to the addition operator, the less-than operator compares arguments as strings only if both of them are strings. To put it more formally, here are the steps:
- Both arguments are converted to primitives. Lets call them
A
andB
. - If both of primitives are Strings, compare
A
andB
as strings. - Otherwise compare
A
andB
as numbers.
For example:
1
8 >
"5"
➙ 8 > 5 ➙
true
;
2
8 >
true
➙ 8 > 1 ➙
true
;
3
"8"
>
"18"
➙
true
;
4
Strict Equals
The favourite operator of many, also known as triple equal (===
) does things in a very simple way: checks if the arguments are of the same type and if they are, checks if they are equal. His little brother has a little bit more complicated character.
Equals
Ok, here it comes, the most hated operator of the language. According to the spec it works like so:
- First check for types, if they are the same, apply strict equals.
- If both arguments are either
null
orundefined
, returntrue
. - If one of them is String and the other is Number, convert both to Number and apply strict equals.
- If one of them is Boolean, convert it to Number and go to 1.
- If one of them is String or Number and the other one is Object, convert object into primitive and go to 1.
- Return
false
.
This basically means that equals works like less-than, when the types of the arguments are different and like strict equals, when types are the same. The easy way to remember: when the types are different it converts both arguments into primitives, then into numbers, unless they both are strings. Oh, and null == undefined
is true
.
1
8 ==
"5"
➙ 8 == 5 ➙
false
;
2
1 ==
true
➙ 1 == 1 ➙
true
;
3
4
0 ==
""
➙ 0 == 0 ➙
true
;
5
0 ==
"0"
➙ 0 == 0 ➙
true
;
6
""
==
"0"
➙
false
;
7
8
"1000"
==
"1e3"
➙
false
;
9
1000 ==
"1e3"
➙
true
;
10
5 == {valueOf:
function
() {
return
5; }} ➙ 5 == 5 ➙
true
;
11
These are not all the operators, but certainly the most tricky ones.
转自:http://blogs.adobe.com/webplatform/2012/09/21/javascript-operators/
- Javascript:Operators
- Javascript常用运算符(Operators)
- Operators
- Operators
- Operators
- Operators
- JavaScript学习笔记-- 运算符OPERATORS
- JavaScript operators( from Javascrip: The definitive guide, 4th guide)
- Javascript常用运算符(Operators)算术运算符
- Eloquent JavaScript 笔记 一、Values, Types, and Operators
- XPath Operators
- 14.2 Operators
- 17.9 Operators
- Oracle Operators
- Conversion Operators
- C++ Operators
- Conversion Operators
- boost operators
- sql收集
- Tuscany_SCA进行SOA开发学习笔记(第一节)
- 关于modelsim的波形文件
- bluez蓝牙协议
- js弹出窗口特效
- Javascript:Operators
- iOS中定时器NSTimer的使用
- Python队列、栈学习
- 山东轻工业学院CSDN高校俱乐部参加“新一代语音云发布暨语音开发者大会”与“2012百度开发者大会”体会心得
- SH Application Binary Interface for GCC
- SYS_CONNECT_BY_PATH 学习
- Hyper-v相关补丁(错误号87 Virtual Machine Management服务无法启动)
- android bundler机制
- [转载]深度学习(Deep Learning)综述及算法简介[转]