JavaScript 表示式 – JavaScript 完全手冊(2018版)
小編推薦: ofollow,noindex">掘金是一個面向程式員的高質量技術社群,從 一線大廠經驗分享到前端開發最佳實踐,無論是入門還是進階,來掘金你不會錯過前端開發的任何一個技術乾貨。
注:本文為 《 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()