您的位置:首页 > Web前端 > JavaScript

JAVASCRIPT基础学习篇(7)--ECMAScript Basic3(EcmaScript 运算符)

2009-02-24 10:03 721 查看
第三章 运算符

1、The instanceof operator

var oStringObject = new String(“hello world”);
alert(oStringObject instanceof String); //outputs “true”

2、Operators

Unary operators(一元)
(1) delete

var o = new Object;
o.name = “Nicholas”;
alert(o.name); //outputs “Nicholas”
delete o.name;
alert(o.name); //outputs “undefined”
delete 不能用于删除固有的属性和方法如:delete o.toString()

(2) void
The void operator returns undefined for any value
<a href=”javascript:window.open(‘about:blank’)”>Click Me</a>
上面的语句放在html中点击Click me后出打开一个空的web页面,同时原页面中会显示[object],这是因为js有一个指向新页面的引用,并转換成字符串显示出来[object]
为了避免这种情况可使用:<a href=”javascript:void(window.open(‘about:blank’))”>Click Me</a>

3、Prefix increment/decrement

var iNum = 10;
++iNum
The second line increments iNum to 11. This is effectively equal to:
var iNum = 10;
iNum = iNum + 1;

--iNum;
alert(iNum); //outputs “9”
alert(--iNum); //outputs “8”
alert(iNum); //outputs “8”

var iNum1 = 2;
var iNum2 = 20;
var iNum3 = --iNum1 + ++iNum2; //equals 22
var iNum4 = iNum1 + iNum2; //equals 22

4、Postfix increment/decrement

var iNum = 10;
iNum--;
alert(iNum); //outputs “9”
alert(iNum--); //outputs “9”
alert(iNum); //outputs “8”

5、Unary plus and minus(一元加与减)

注意:=+ 与+=的区别,这里说的是 =+
=+对number类型无效,如以下并没有改变iNum的值
var iNum= 25;
iNum = +iNum;
alert(iNum); //outputs “25”

=+对String类型有效,会自动转換类型,从String到Number,但值未改变
var sNum = “25”;
alert(sNum) //outputs 25
alert(typeof sNum); //outputs “string”
var iNum = +sNum;
alert(sNum) //outputs 25
alert(typeof iNum); //outputs “number”

var iNum= 25;
iNum = -iNum;
alert(iNum); //outputs “-25”

var sNum = “25”;
alert(typeof sNum); //outputs “string”
var iNum = -sNum;
alert(iNum); //outputs “-25”
alert(typeof iNum); //outputs “number”

6、Bitwise operators(位)

求25的二进制数:var iNum = 25; iNum.toString(2)
正数的原码、反码、补码
负数的补码求法:对负数的绝对值的原码取反再加1
位运算有:&,|,~,^(XOR)
Left shift左移:左移不分带符号和不带符号,都在原来值上面乘以2的n次方,n为移的位数
如:8<<2=32,-8<<2=-32, 9<<2 = 36,-9<<2=-36
Right shift分带符号和不带符号两种:
Signed right shift(>>)带符号右移,指最高位为符号位,右移n位即在左边补n个1.
Unsigned right shift(>>>)不带符号右移,指右移n位在左边补n个0.
8>>2=2, -8>>2=-2, 9>>2=2, -9>>2=-3
8>>>2=2 -8>>>2=1073741822 (按32位计算) 9>>>2=2; -9>>>2=1073741821 (按32位计算)
上面对于正数的>>和>>>都很好理解,就是不断的整除2得到的结果.有三个比较难理解的地方:
(1)为什么-9>>2=-3.这里面涉及求反码及补码的过程(因为数值在计算机里面是以补码表示的)
-9的补码:9的原码(00001001)求反码(11110110)再加1(11110111)
或者-9的原码(10001001)除符号求反码(11110110)再加1(11110111)两种求法得到相同的结果
那么将11110111>>2注意是符号右移即在左边补1,得到11111101.但得到的值11111101是机器码,也即补码形式,那么转换成原码再求值
实际上就是对11111101求补码,即除符号求反码10000010再加1=10000011,最终结果为-3
(2)为什么-8>>>2=1073741822 (按32位计算).
-8的原码32位形式:10000000 00000000 00000000 00001000
-8的反码32位形式:11111111 11111111 11111111 11110111
-8的补码32位形式:11111111 11111111 11111111 11111000
将补码无符号右移,即左补0,因为符号位变成了0,所以得到的是一个正数
00111111 11111111 11111111 11111110
因为正数的补码即原码,所以转换后的值为00111111 11111111 11111111 11111110化成十进制为1073741822
(3)为什么-8>>2=-2.
-8的原码32位形式:10000000 00000000 00000000 00001000
-8的反码32位形式:11111111 11111111 11111111 11110111
-8的补码32位形式:11111111 11111111 11111111 11111000
将补码带符号右移,即左补1得
11111111 11111111 11111111 11111110
求11111111 11111111 11111111 11111110的补码,因为最高位符号位为1所以为负数,补码的补码=原码
所以求二进制补码 11111111 11111111 11111111 11111110的原码,即对它求补码
先求反码,符号不变10000000 00000000 00000000 00000001
再加1得原码 10000000 00000000 00000000 00000010
最终结果为-2

7.Boolean operators

Logical NOT
If the operand is an object, false is returned.
If the operand is the number 0, true is returned.
If the operand is any number other than 0, false is returned.
If the operand is null, true is returned.
If the operand is NaN, true is returned.
If the operand is undefined, an error occurs.
Logical AND
var bTrue = true;
var bFalse = false;
var bResult = bTrue && bFalse;
注意:Logical AND can be used with any type of operands, not just Boolean values. When either operand is not a primitive Boolean, logical AND does not always return a Boolean value:
If one operand is an object and one is a Boolean, the object is returned.
If both operands are objects, the second operand is returned.
If either operand is null, null is returned.
If either operand is NaN, NaN is returned.
If either operand is undefined, an error occurs.
Logical OR
var bTrue = true;
var bFalse = false;
var bResult = bTrue || bFalse;
注意:Just like logical AND, if either operand is not a Boolean, logical OR will not always return a Boolean value:
If one operand is an object and one is a Boolean, the object is returned.
If both operands are objects, the first operand is returned.
If both operands are null, null is returned.
If either operand is NaN, NaN is returned.
If either operand is undefined, an error occurs.

8.+,-,*,/

Multiply(*)
var iResult = 34 * 56;
the multiply operator also has some unique behaviors when dealing with special values:
If the operands are numbers, regular arithmetic multiply is performed, meaning that two positives or two negatives equal a positive, whereas operands with different signs yield a negative.
If the result is too high or too low, the result is either Infinity or –Infinity.
If either operand is NaN, the result is NaN.
If Infinity is multiplied by 0, the result is NaN.
If Infinity is multiplied by any number other than 0, the result is either Infinity or –Infinity, depending on the sign of the second operand.
If Infinity is multiplied by Infinity, the result is Infinity.
Divide(/)
var iResult = 66 / 11;
需要注意的是:在JS中/只是数学里面的除法操作而不是JAVA中的整除,如在js中:8/3=2.6666666665,在java中为2
special behaviors for special values:
If the operands are numbers, regular arithmetic division is performed, meaning that two positives or two negatives equal a positive, whereas operands with different signs yield a negative.
If the result is too high or too low, the result is either Infinity or – Infinity.
If either operand is NaN, the result is NaN.
If Infinity is divided by Infinity, the result is NaN.
If Infinity is divided by any number, the result is Infinity.Division of a non-infinite number by 0 always equals NaN.
If Infinity is divided by any number other than 0, the result is either Infinity or –
Infinity, depending on the sign of the second operand.
Modulus(%)
var iResult = 26 % 5; //equal to 1
the modulus operator behaves differently for special values:
If the operands are numbers, regular arithmetic division is performed, and the remainder of that division is returned.
If the dividend is Infinity or the divisor is 0, the result is NaN.
If Infinity is divided by Infinity, the result is NaN.
If the divisor is an infinite number, the result is the dividend.
If the dividend is 0, the result is 0.
Add
If either number is NaN, the result is NaN.
If Infinity is added to Infinity, the result is Infinity.
If –Infinity is added to –Infinity, the result is –Infinity.
If Infinity is added to –Infinity, the result is NaN.
If +0 is added to +0, the result is +0.
If –0 is added to +0, the result is +0.
If –0 is added to –0, the result is –0.

If, however, one of the operands is a string, then the following rules are applied:
If both operands are strings, the second string is concatenated to the first.
If only one operand is a string, the other operand is converted to a string and the result is the concatenation of the two strings.
这里需要注意一下+=的区别:看上面第5点
Subtract
If the two operands are numbers, perform arithmetic subtract and return the result.
If either number is NaN, the result is NaN.
If Infinity is subtracted from Infinity, the result is NaN.
If –Infinity is subtracted from –Infinity, the result is NaN.
If –Infinity is subtracted from Infinity, the result is Infinity.
If Infinity is subtracted from –Infinity, the result is –Infinity.
If +0 is subtracted from +0, the result is +0.
If –0 is subtracted from +0, the result is –0.
If –0 is subtracted from –0, the result is +0.
If either of the two operands is not a number, the result is NaN.

9.Relational operators

一般情况下字符串比较的比较每一个字符在字母表中出现的先后顺序如
var a = "a"
var b = "b"
alert(a<b) //outputs true
var a = "ab"
var b = "b"
alert(a<b) //outputs also true
上面例子说明JS里面比较是一个字母一个字母的比较,一旦比较有了结果就将其作为最终的结果:如
var a = "afsfsf"
var b = "bcsfsf"
alert(a<b) //outputs true
但是要注意的是:这里的比较并不是严格按字母先后顺序比较的,当字母分大小写的时候,大写的字母因为ascii码是小于相应的小写字母的如:
var a = "Bsfsfsf";
var b = "bs";
alert(a<b) //outputs true
所以比较的时候务必转換成一致的格式
var a = "Bsfsfsf";
var b = "bs";
alert(a.toLowerCase()<b.toLowerCase()) //outputs false
还有需要注意的地方如下:
var a = "23"
var b ="3"
alert(a>b) //outputs false
这个容易理解,因为都是字符串,2的ascii50,3的ascii为51,所以这里a<b

var a = "23"
var b = 3;
alert(a<b) //outputs false
Whenever a number is compared to a string, ECMAScript says that the string should be converted into a number and then numerically compared with the other number
当比较的时候只要有一个数是number类型的,那么将把另一个转换成number类型进行比较这里将“23”转換为23,然后23>3结果是我们所期望的。那么假如字符串不能转換为number类型应该怎么办呢?
var bResult = "a" < 3;
alert(bResult) //outputs false
那么再看下面:
var bResult = "a" >= 3;
alert(bResult)这个结果会是什么呢?
//outputs false
那么为什么"a" < 3;与"a" >= 3;返回的都是false呢。这是因为JS规定只要其中一个字符串不能转換为number类型即为NaN时,那么结果将为false
As a rule, any relational operation that contains NaN returns false

10.Equality operators

JS中考虑比较基本类型及对象类型的情况分别规定,
equal and not equal to deal with primitive values (==),(!=)
identically equal and not identically equal to deal with objects. (===),(!==)

Equal and not equal
js规定在进行Equal比较前,两个操作数必需转换成相同的类型,相应转換规则如下:
If an operand is a Boolean value, convert it into a numeric value before checking for equality.A value of false converts to 0; whereas a value of true converts to 1.
If one operand is a string and the other is a number, attempt to convert the string into a number before checking for equality.
If one operand is an object and the other is a string, attempt to convert the object to a string (using the toString() method) before checking for equality.
If one operand is an object and the other is a number, attempt to convert the object to a number before checking for equality.
相应的比较规则如下:
Values of null and undefined are equal.
Values of null and undefined cannot be converted into any other values for equality checking.
If either operand is NaN, the equal operator returns false and the not equal operator returns true. Important note: Even if both operands are NaN, the equal operator returns false because,by rule, NaN is not equal to NaN.
If both operands are objects, then the reference values are compared. If both operands point to the same object, then the equal operator returns true. Otherwise, the two are not equal.
特殊情况的比较规则
null == undefined true
“NaN” == NaN false
5 == NaN false
NaN == NaN false
NaN != NaN true
false == 0 true
true == 1 true
true == 2 false
undefined == 0 false
null == 0 false
“5” == 5 true
Identically equal and not identically equal

Identically equal (===)returns true if the operands are equal without conversion
即不经过转换而得到的比较结果
var sNum = “55”;
var iNum = 55;
alert(sNum == iNum); //outputs “true”.its equal after convert String to number
alert(sNum === iNum); //outputs “false”

(!==) returns true only if the operands are not equal without conversion
var sNum = “55”;
var iNum = 55;
alert(sNum != iNum); //outputs “false”
alert(sNum !== iNum); //outputs “true”

11.Conditional operator

variable = boolean_expression ? true_value : false_value;

12.Assignment operators

Simple assignment(=)
Compound assignment(+=)
Multiply/Assign (*=)
Divide/Assign (/=)
Modulus/Assign (%=)
Add/Assign (+=)
Subtract/Assign (-=)
Left Shift/Assign (<<=)
Signed Right Shift/Assign (>>=)
Unsigned Right Shift/Assign (>>>=)
13.Comma operator

The comma operator allows execution of more than one operation in a single statement. Example:
var iNum1=1, iNum2=2, iNum3=3;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: