JavaScript 表达式

161 阅读4分钟

在我的认知中,编程是使用计算机对现实世界进行抽象,提取出一些 关键特征 ,使用这些 关键特征 建立 数学模型,然后进行 计算 ,获取我们想要的信息,再用它指导我们去对现实世界作出反应。

所以,我认为编程的核心在于 计算,编程语言是用来 表达计算 的语言。

数据类型用来描述特征;数据结构是特征的组合,是现实映射到数学上的模型;算法是计算的步骤,好的算法可以降低 计算 耗费的能量,坏的算法则需要更多的能量来得到相同的信息。

根据这一认知,程序应该是由 无所不在的计算 组成的,恰好,编程语言中有一个直观反应 计算 的概念:表达式

为此,我打算读一遍 《Javascript: The Definitive Guide》中关于 Expressions and Operators 的部分,本文为这部分的读书笔记。

1. 什么是表达式?

  • 表达式可以被求值,是最基础的计算单元
  • 简单的表达式可以组合成更复杂的表达式。

2. 基本表达式 Primary Expressions

最简单的表达式,不能被分解成更简单的表达式的表达式,称为基本表达式。

  • 常量

    1.23 // A number literal
    "hello" // A string literal
    /pattern/ // A regular expression literal
    
  • 关键字

    true   // Evaluates to the boolean true value
    false  // Evaluates to the boolean false value
    null   // Evaluates to the null value
    this   // Evaluates to the "current" object
    undefined // Evaluates to the undefined value
    
  • 变量

    i // Evaluates to the value of variable i
    sum // Evaluates to the value of varaible sum
    

3. 对象和数组字面量 Object and Array Initializers

对象和数组字面量也是表达式,其求值的结果是一个新建的对象和数组。

3.1 数组字面量 Array literal

[] // Evaluates to an array with 0 elements
[1+2, 3+4] // A 2-element array. Fisrt element is 3, second is 7

3.2 对象字面量 Object literal

{} // Evaluates to an object with no properties
{ x: 1, y: 2 } // Evaluates to an object with 2 properties

4. 函数定义表达式 Function Definition Expressions

一个函数定义表达式的求值结果是一个新定义的函数,函数定义表达式也可称作函数字面量 (function literal)

function (x) { return x * x } // Evaluates to a newly defined anonymous function

function square(x) { return x * x } // Evaluates to a newly defined function whit name square

(a, b) => { return a + b } // Evaluates to a newly defined arrow function 

5. 属性读取表达式 Property Access Expressions

5.1 普通属性读取表达式

一个属性读取表达式用来对一个对象的属性或一个数组的元素进行求值:

expression . identifier
expreesion [ expression ] 

在上面的规则中, .[ 之前的表达式先进行求值,如果求值的结果是 null 或者 undefined ,那么这个表达式会抛出一个 TypeError ,因为 nullundefined 不能拥有属性。

let obj = {
    x : 1,
    y: { z: 3 }
}
let arr = [ obj, 4, [5, 6]]

obj.x // Evaluates to 1
obj.y.z // Evaluates to 3
obj["x"] //  Evaluates to 1
arr[1] // Evaluates to 4
arr[2]["1"] // Evaluates to 6
arr[0].x // Evaluates to 1

5.2 条件属性读取 Conditional Property Access

ES2020 添加了两种新的属性读取表达式:

expression ?. identifier
expression ?. [ expression ]

如果第一个表达式求值的结果是 nullundefined ,那么就会停止对后面的表达式进行求值。

否则,继续进行求值。

举个例子:

let a = { b: null }

a.b.c // Throws a TypeError
a.b?.c // Evaluates to the null value 

6. 函数/方法调用表达式 Invocation Expressions

调用表达式用来调用一个函数或一个方法,由两部分组成:

(1)函数表达式

(2)一对括号,里面是一些用逗号分隔的参数表达式

f(0) // f is the function expression; 0 is the argument expression
Math.max(x, y, z) // Math.max is the function; x, y and z are the argumeent expressions
a.sort() // a.sort is the function; there are no arguments

7. 对象创建表达式 Object Creation Expressions

new Object()
new Point(2, 3)

8. 操作符 Operators

操作符用来构建更复杂的计算过程

expressions.svg

9. 总结

  • 表达式是编程语言的基本核心概念,用来表达计算。
  • 表达式不仅限于加减乘除这类算术操作,也包括变量求值,对象, 数组声明求值和函数求值等。
  • 使用操作符可以构建出更复杂的表达式,也就是进行更复杂的计算过程。

参考文献

  • Javascript: The Definitive Guide,Seventh Edition, Chapter 4, p61-96.