你的浏览器不支持canvas

Love You Ten Thousand Years

JavaScript的表达式和运算符

Date: Author: M/J

本文章采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可。转载请注明来自小可嗒嗒的博客

常见的表达式和运算符都已经了解了,所以主要是一些不同的以及需要注意的点。

Destructuring – 解构

对于更复杂的赋值, 这个解构赋值destructuring assignment语法是一个能从数组或对象对应的数组结构或对象字面量里提取数据的Javascript表达式。

var foo = ["one", "two", "three"];

// 不使用解构
var one   = foo[0];
var two   = foo[1];
var three = foo[2];

// 使用解构
var [one, two, three] = foo;

求模与求余运算

求余运算符% – Remainder

JavaScript中的%就是求余运算,返回求余结果。

即:result = number1 % number2

返回的符号由number1的符号确定,返回值的大小在0number2的绝对值之间。

-19 / -2  // output: -1
19 / 6.7    //output: 5.6

JavaScript中的/是普通的除法运算。

5 / 3   //output: 1.6666666666666667
-5 /-3  //outpuy: 1.6666666666666667

求模运算

JavaScript中的求模运算可以采用以下方法实现:

Number.prototype.mod = function (n){
    return ((this % n) + n) % n;
};
console.log((-13).mod(64));  // 51

总结

对于整型数ab来说,取模运算或者求余运算的方法都是:

  • 求整数商: c = a/b
  • 计算模或者余数: r = a - c*b.

求模运算和求余运算在第一步不同: 取余运算在取c的值时,向0方向舍入;而取模运算在计算c的值时,向负无穷方向舍入。

举例: 计算-7 Mod 4, 那么a = -7; b = 4

  • 第一步:求整数商c,进行求模运算 c = -2(向负无穷方向舍入),进行求余运算 c = -1(向0方向舍入)
  • 第二步:由公式r = a - c*b,由于c值不一样,求模时r = 1,求余时r = -3

因此,当ab符号一致时,求模运算和求余运算所得的c的值一致,因此结果一致。当符号不一致时,结果不一样。求模运算结果的符号和b一致,求余运算结果的符号和a一致。

不同语言对%的含义是不同的,Python取模C/C++取余JavaScript取余

若只想要要得到整数商呢?

JavaScript有一个全局对象parseInt()可以帮助我们解决这个问题。

语法是:

parseInt (string, radix)

内置对象Number的方法parseInt()和这个结果是一致的。

parseInt(7 /2); // 3

JavaScript的内置Math还提供一些其他的方法:

  • Math.ceil(x): 返回向上取整
  • Math.round(x): 返回四舍五入后的整数
  • Math.floor(x): 返回向下取整

Exponentiation operator – 指数运算符

JavaScript还提供指数运算符**

2 ** 3  // 8

Conditional operator – 条件运算符

语法如下:

condition ? val1 : val2

If condition is true, the operator has the value of val1. Otherwise it has the value of val2.

Unary operators – 一元操作符

delete

The delete operator deletes an object, an object’s property, or an element at a specified index in an array.(删除一个对象,或一个对象的属性,或一个数组中的元素或键值) The syntax is:

delete objectName;
delete objectName.property;
delete objectName[index];
delete property;

The fourth form is legal only within a with statement, to delete a property from an object.

你能使用delete删除各种各样的隐式声明(implicity declared), 但是被var声明的除外。

如果delete操作成功, 属性或者元素会变成undefined. 如果delete可行会返回true,如果不成功返回false.

x = 42;
var y = 43;
myobj = new Number();
myobj.h = 4;    // create property h
delete x;       // returns true (can delete if declared implicitly)
delete y;       // returns false (cannot delete if declared with var)
delete Math.PI; // returns false (cannot delete predefined properties)
delete myobj.h; // returns true (can delete user-defined properties)
delete myobj;   // returns true (can delete if declared implicitly)

Deleting array elements – 删除数组元素

删除数组中的元素时,数组的长度是不变的,例如delete a[3], a[4]a[4]a[3]仍然存在,只是变成了undefined.

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
if (3 in trees) {
  // this does not get executed(不会被执行)
}

如果想让数组中存在一个元素但是是undefined值,使用undefined关键字而不是delete操作.

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
if (3 in trees) {
  // this gets executed(会被执行)
}

typedof

typeof的语法形式如下:

typeof operand
typeof (operand)

void

void (expression)
void expression

void运算符,表明一个运算没有返回值。

下面的代码创建了一个超链接,当用户单击它时,提交一个表单。

<a href="javascript:void(document.form.submit())">
Click here to submit</a>

Relational operators – 关系操作符

in operator

如果指定的属性(property)在指定的对象(object)中会返回true,语法如下:

propNameOrNumber in objectName

例子:

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees;        // returns true
3 in trees;        // returns true
6 in trees;        // returns false
"bay" in trees;    // returns false (you must specify the index number,
                   // not the value at that index)
"length" in trees; // returns true (length is an Array property)

// Predefined objects
"PI" in Math;          // returns true
var myString = new String("coral");
"length" in myString;  // returns true

// Custom objects
var mycar = {make: "Honda", model: "Accord", year: 1998};
"make" in mycar;  // returns true
"model" in mycar; // returns true

instanceof

instanceof operator, 如果对象是某种指定类型(object type)返回true。语法如下:

objectName instanceof objectType

当你需要确认一个对象在运行时的类型时使用instanceof。例如, 抓取异常, 你可以根据抛出异常的类型分类处理异常代码。

var theDay = new Date(1995, 12, 17);
if (theDay instanceof Date) {
  // statements to execute 执行
}

Expressions – 表达式

An expression is any valid unit of code that resolves to a value.

·JavaScript· has the following expression categories:

  • Arithmetic: evaluates to a number, for example 3.14159. (Generally uses arithmetic operators.)
  • String: evaluates to a character string, for example, “Fred” or “234”. (Generally uses string operators.)
  • Logical: evaluates to true or false. (Often involves logical operators.)
  • Primary expressions: Basic keywords and general expressions in JavaScript.
  • Left-hand-side expressions: Left values are the destination of an assignment.

Primary expressions

this

使用this keyword关键字来指代当前对象(current object),通常,this指代的是方法中正在被调用的对象。用法如下

this["propertyName"]
this.propertyName

Left-hand-side expressions – 左值表达式

Left values are the destination of an assignment.

new

你可以使用new operator 创建一个自定义类型或者是预置类型的对象实例。用法如下:

var objectName = new objectType([param1, param2, ..., paramN]);

super

super关键字可以用来调用一个对象父类的函数,它在用来调用一个类的父类的构造函数时非常有用。

super([arguments]); // calls the parent constructor. super.functionOnParent([arguments]);

Spread operator

spread operator允许一个表达式在原地展开,当需要多个参数(比如函数调用时)或者多个值(比如字面量数组)。


对于本文内容有问题或建议的小伙伴,欢迎在文章底部留言交流讨论。