解构
解构是一种从对象和数组中获取值的快速方法。例如,可以使用一行代码提取值并将它们分配给变量。
解构可以在对象上或数组上作用。
1 2 3 4 5 6 7 8 9
| const person = { name: 'John Doe', age: 32, location: 'San Francisco' };
const { name, age, location } = person;
console.log(name, age, location); // John Doe 32 San Francisco
|
1 2 3 4 5
| const colors = ['red', 'green', 'blue'];
const [first, second, third] = colors;
console.log(first, second, third); // red green blue
|
块作用域
可以使用块作用域来声明仅在特定代码块内可用的变量。在 JavaScript 中有两种声明变量的方法:var和let。
var 关键字声明一个全局或函数范围的变量,这意味着它可以从同一函数内的任何地方访问。另一方面,let 关键字声明了一个块作用域的变量,这意味着它只能在同一代码块内访问。
1 2 3 4 5 6
| if (true) { let message = 'Hello, world!'; console.log(message); // Hello, world! }
console.log(message); // Uncaught ReferenceError: message is not defined
|
传播运算符
模板文字
箭头函数