Home > Web Front-end > JS Tutorial > body text

javascript core language - expressions and operators

高洛峰
Release: 2016-10-17 09:47:06
Original
1205 people have browsed it

An expression is a phrase in JavaScript, and the JavaScript interpreter will evaluate it to produce a result. Constants, variable names, array access, etc. in the program are all expressions. The most common way to combine simple expressions into complex expressions is to use operators. The original expression is the simplest expression. "Primary expression". Is the smallest unit of expression ———— No other expressions are included. Constants, literals, keywords, and variables are all primitive expressions

1.23
"hello"
/pattern/

true
false
null
this

i
sum
undefined
Copy after login

Initialization expressions for objects and arrays

Initialization expressions for objects and arrays are actually a newly created object and array. These expressions are sometimes called Make "object literals" and "array literals"!

[]
[1+2, 3+4]

var sparseArray = [1,,,,,5]           // 数组分割逗号之前的元素可以省略,空位默认填充 undefined
var matrix = [[1,2,3], [4,5,6], [7,8,9]]

var p = { x: 2.3, y: -1.2}
var q = {}
q.x = 2.3; q.y = -1.3
Copy after login

Function definition expression

var square = function(x) { return x*x }
Copy after login

Property access expression

// expression.identifier
// expression[expression]

var o = { x:1, y:{z:3} };
var a = [0, 4, [5, 6]];
o.x             // => 1 表达式 o 的 x 属性
o.y.z           // => 3 表达式 o.y 的属性 z
o["x"]          // => 1 对象 o 的 x 属性
a[1]            // => 4 表达式 a 中索引为 1 的元素
a[2]["1"]       // => 6 表达式 a[2] 中索引为 1 的元素
a[0].x          // => 1 表达式 a[0] 的 x 属性

/*不管使用哪种形式的属性访问表达式,在「.」和「[」 之前的表达式总是会首先计算。如果计算结果是 null 或者 undefined,表达式会抛出一个类型错误异常,因为这两个值都不能包含任意属性。如果运算结果不是对象(或者数组),JavaScript 会将其转换为对象。如果对象表达式后跟随一对方括号,则会计算方括号内的表达式的值并将它转换为字符串,不论哪种情况,如果命名的属性不存在,那么整个属性访问表达式的值就是 undefined */
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template