JavaScript 表达式 – JavaScript 完全手册(2018版)

10年服务1亿Tian开发工程师

注:本文为 《 JavaScript 完全手册(2018版) 》第10节,你可以查看该手册的完整目录。

表达式是可以计算并解析为值的代码单元。JS中的表达式可以分为几类。

算术表达式

在此类别下,将所有评估为数字的表达式:

1 / 2
i++
i -= 2
i * 2

字符串表达式

评估为字符串的表达式:

'A ' + 'string'
'A ' += 'string'

基本表达

在这个类别下去变量引用,字面量和常量:

2
0.02
'something'
true
false
this //the current object
undefined
i //where i is a variable or a constant

还有一些语言关键词:

function
class
function* //the generator function
yield //the generator pauser/resumer
yield* //delegate to another generator or iterator
async function* //async function expression
await //async function pause/resume/wait for completion
/pattern/i //regex
() // grouping

数组和对象初始化表达式

[] //array literal
{} //object literal
[1,2,3]
{a: 1, b: 2}
{a: {b: 1}}

逻辑表达式

逻辑表达式使用逻辑运算符并解析为布尔值:

a && b
a || b
!a

左边的表达式

new //create an instance of a constructor
super //calls the parent constructor
...obj //expression using the spread operator

属性访问表达式

object.property //reference a property (or method) of an object
object[property]
object['property']

对象创建表达式

new object()
new a(1)
new MyRectangle('name', 2, {a: 4})

函数定义表达式

function() {}
function(a, b) { return a * b }
(a, b) => a * b
a => a * 2
() => { return 2 }

调用表达式

调用函数或方法的语法

a.x(2)
window.resize()
赞(4) 打赏
未经允许不得转载:WEBTian开发 » JavaScript 表达式 – JavaScript 完全手册(2018版)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Tian开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏