`

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

阅读更多

第三章 运算符


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;

分享到:
评论

相关推荐

    计算机软件-编程源码-Javascript_VBScript教程专栏.zip

    1. **基础语法**:变量声明(var、let、const)、数据类型(基本类型与引用类型)、运算符、流程控制(条件语句、循环)等。 2. **函数**:函数定义、函数表达式、作用域、闭包。 3. **面向对象**:对象、类、原型链...

    Basic-knowledge-of-java.zip_Knowledge

    以上是JavaScript基础知识的概览,通过深入学习和实践,你可以更好地掌握这门强大的脚本语言。对于压缩包中的"Basic knowledge of java.doc"文档,很可能是针对JavaScript的基础知识进行的详细讲解,包括上述知识点...

    basic-course-javascript

    【JavaScript基础课程】 JavaScript,简称JS,是一...这个"basic-course-javascript"课程将涵盖以上这些关键知识点,并通过实例和练习帮助学习者逐步掌握JavaScript编程,为他们进一步探索Web开发世界打下坚实的基础。

    「大学Javascript网络课考试题答案」.docx

    - **解析:** JavaScript具备脚本编写语言的特点,易于学习,同时它还具有简单性、动态性、安全性以及跨平台性等优点。 **3. JavaScript与Java的区别** - **知识点:** JavaScript与Java的区别在于它们是基于对象与...

    《JavaScript 语言参考》中文版

    综上所述,《JavaScript 语言参考》中文版不仅介绍了JavaScript的基础知识,还涉及了其实际应用和与其他技术的关联,对于任何想要深入学习JavaScript的开发者来说,都是一份宝贵的参考资料。通过深入学习和实践书中...

    js-basic-workshop:关于JS基本知识的讲习班

    以上只是JavaScript基础工作坊的一部分内容,实际学习中还应涉及错误处理、类型转换、模块化开发、前端框架等内容。通过这个讲习班,你将建立起坚实的语言基础,为进一步的Web开发打下牢固的根基。

    VBScript+JavaScript+HTML参考手册

    6. **ECMAScript标准**:JavaScript的标准化版本,最新为ECMAScript 2022。 JavaScript的核心概念包括变量、数据类型(如Number、String、Boolean、Object、Null、Undefined)、运算符、控制结构、函数、对象和数组...

    经典js和vbs的教程,微软出品

    1. **基础语法**:包括变量声明、数据类型、运算符、流程控制(条件语句和循环)、函数等基础知识。 2. **对象模型**:JavaScript中的DOM(文档对象模型)允许操作HTML元素,VBScript则有WScript对象、...

    网页开发教程

    JavaScript是ECMAScript标准的一个实现,而VBScript是基于Microsoft的Visual Basic。JavaScript跨平台且更灵活,被广泛接受和使用;而VBScript主要局限于Microsoft的IE浏览器,现在已较少使用。 ### 知识点二:...

    WINDOWS脚本技术参考手册

    1. **基础语法**:介绍JavaScript、JScript和VBScript的基本语法,包括变量声明、数据类型、运算符、流程控制语句、函数等。 2. **对象模型**:详细讲解各种环境下的对象模型,如DOM(Document Object Model)用于...

    VBScript与JScript实例教程1-4

    它是Visual Basic家族的一部分,由Microsoft公司开发,与JavaScript类似,但两者并不完全相同。JScript是Microsoft对ECMAScript标准的实现,而VBScript则是微软特有的一种脚本语言。 本教程“VBScript与JScript实例...

    es6-basic

    **ES6(ECMAScript 2015)基本知识点详解** ES6,全称ECMAScript 2015,是JavaScript语言的一个重大更新...在学习和实践中,理解这些新特性的原理及其应用场景,能够帮助开发者写出更高效、更易维护的JavaScript代码。

    CursoIEEE2014:线上课程平台

    3. **运算符**:JavaScript支持算术运算符、比较运算符、逻辑运算符等,如`+`、`-`、`*`、`/`、`==`、`===`、`&&`、`||`等。 4. **控制流语句**:包括条件语句(if...else)、循环语句(for、while、do...while)...

    bc_basic_js

    "bc_basic_js"这个压缩包很可能包含了一系列关于JavaScript基础知识的教程或者示例代码,旨在帮助学习者理解并掌握JS的基本概念和用法。 JavaScript主要由以下几个核心部分组成: 1. **变量与数据类型**:...

    entryjs:入门相关的JS集合

    JavaScript,简称JS,是一种广泛用于网页和网络应用的脚本语言,它是基于ECMAScript规范的。在Web开发领域,JavaScript是不可或缺的一部分,它赋予了网页动态交互的能力,使得用户与网页之间的交互变得更加直观和...

    javascript入门笔记

    Javascript Basic 1、Javascript 概述(了解) Javascript,简称为 JS,是一款能够运行在 JS解释器/引擎 中的脚本语言 JS解释器/引擎 是JS的运行环境: 1、独立安装的JS解释器 - NodeJS 2、嵌入在浏览器中的JS...

    210224_NomadCode_VanillaJS_basic:210224 Nomad Code로Vanilla JS기초ON

    1. **基础语法**:首先,课程会介绍JavaScript的基本语法,如变量声明(var、let、const)、数据类型(number、string、boolean、object等)、运算符(算术、比较、逻辑等)以及流程控制(条件语句、循环结构)。...

    JScript中文参考手册

    1. **基础语法**:变量声明、数据类型、运算符、流程控制语句(如if、for、while)、函数定义和调用等。 2. **内置对象**:讲解Date、Array、String等内置对象的属性和方法。 3. **DOM操作**:如何使用JScript操作...

    ASP速查手册(VBScript+JScript)

    VBScript语法与Visual Basic类似,易于学习,支持变量、常量、数据类型、运算符、流程控制语句以及函数等基本概念。例如,以下是一个简单的VBScript ASP脚本段: ```vbscript Dim message message = "欢迎访问!" ...

    windows script 中文帮助(含vbs,js)

    VBScript 是基于Visual Basic的,而JScript则是对JavaScript的实现,两者都遵循ECMAScript规范。这个“Windows Script 中文帮助”文件可能是为开发者提供的官方文档的中文版,包含了这两种语言的详细说明、语法参考...

Global site tag (gtag.js) - Google Analytics