santisify Site

Back

解构#

数组解构#

完全解构

let a, b;
[a, b] = [1, 2]
console.log(a, b); // 1 2
js

部分解构

let [a, b] = [1, 2, 3];
console.log(a, b) // 1 2

let [x, y, z] = [1, 2];
console.log(x, y, z) // 1 2 undefined
js

忽略方式解构

let [, , a] = [1, 2, 3];
console.log(a); // 3
js

嵌套解构

let [a, b, [c]] = ['a', 'b', ['c']];
console.log(a, b, c); //a b c
js

剩余运算符解构

let [a, ...b] = [1, 2, 3, 4, 5]
console.log(a) // 1
console.log(b) // [ 2, 3, 4, 5 ]
js

解构默认值

let [a, b] = [1]
console.log(a, b) // 1 undefined
let [x, y = 2] = [1]
console.log(x, y) // 1 2
let [u, v = 2] = [1, 3]
console.log(u, v) // 1 3
js

使用场景:

  1. 实现两数交换
let a = 10, b = 20
console.log(a, b); // 10 20
[a, b] = [b, a]
console.log(a, b); // 20 10
js
  1. 使用函数返回值
function fn() {
    return [1, 2, 3];
}
let [a, b, c] = fn();
console.log(a, b, c); // 1 2 3
js

对象解构#

完全解构

let obj = {
    name: "John Doe",
    age: 32,
}
{
    let {name, age} = obj;
    console.log(name, age) // John Doe 32
}
{
    let {age, name} = obj;
    console.log(name, age) // John Doe 32
}
js

部分解构

let obj = {
    name: "John Doe",
    age: 32,
}
let {age} = obj
console.log(age) // 32
let {addr, name} = obj
console.log(addr, name)// undefined John Doe
js

解构后重命名

let obj = {
    name: "John Doe",
    age: 32,
}
let {name: username, age: userage} = obj
console.log(username, userage) // John Doe 32
js

剩余运算符解构(浅拷贝)

let obj = {
    name: "John Doe",
    age: 32,
    email: 'xh@example.com'
}
let {name, ...res} = obj
console.log(name) // John Doe
console.log(res) // { age: 32, email: 'xh@example.com' }
js

解构默认值

let obj = {
    name: "John Doe",
    age: 32,
    email: 'xh@example.com'
}
let {name, img = 'a.webp'} = obj
console.log(name, img) // John Doe a.webp
let {age = 10, email} = obj
console.log(age, email) // 32 xh@example.com
js

使用场景: 函数返回值

function fn() {
    return {name: "santisify", age: "22", email: "xh@example.com"}
}
let {name}= fn();
console.log(name) // santisify
js

字符串解构#

let str = "hfkjdsaj"
let [a, b, c, d] = str
console.log(a, b, c, d) // h f k j
let {length} = str
console.log(length) // 8
js

字符串扩展#

模板字符串#

let str = "hello world";

console.log(`hello, ${str} , world!`);// hello, hello world , world!
js

新增方法#

let str = "hello world";
// includes() 判断是否包含 返回 true/false
console.log(str.includes("he")); // true
console.log(str.includes("ho")); // false
// startsWith() 判断目标字符串是否在源字符串头部 返回 true/false
console.log(str.startsWith('h')); // true
console.log(str.startsWith('e')); // false
// endsWith() 判断目标字符串是否在源字符串尾部 返回 true/false
console.log(str.endsWith('ld')); // true
console.log(str.endsWith('l')); // false
// repeat() 将字符串重复n次 返回重复后的字符串
console.log(str.repeat(3));// hello worldhello worldhello world 
js

对象扩展#

新特性#

属性和方法简洁表达方式 若属性名和接收的变量名相同,可直接使用变量名作为属性名 方法 方法名(){}

let router = "router"
let vue = {
    router, // 等同于 router:touter
    data(){
        console.log("fn-data")
    }
    // 等同于
    // data:function(){
    //     console.log("fn-data")
    // }
}
console.log(vue)
vue.data()
js

表达式方式的属性名和方法名

let obj = {
    ['na'+'me']: 'santisify',
    ['say'+'hello']() {
        console.log('hello')
    }
}
console.log(obj.name) // santisify
obj.sayhello() // hello
js

新增方法#

数组扩展#

扩展运算符号

let a = ['a', 'b', 'c']
let b = ['d', 'e', 'f']
{
    let z = [...a, ...b] // arr2 = arr1.concat()
    a.push('d')
    console.log(a, b, z) // [ 'a', 'b', 'c', 'd' ] [ 'd', 'e', 'f' ] [ 'a', 'b', 'c', 'd', 'e', 'f' ]
}
js

新方法

ES6中字符串、对象、数组新特性
https://santisify.top/series/javascript/arrary-object-string
Author santisify
Published at September 8, 2026

本系列 · javascript

  • 01 ES6中字符串、对象、数组新特性 (当前)
查看全部 →