javascript ES6 - 解构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ES6 - 解构相关的知识,希望对你有一定的参考价值。

const person = {
    first: 'Wes',
    last: 'Bos',
    country: 'Canada',
    city: 'Hamilton',
    twitter: '@wesbos',
};

// Destructure an object as values with the keyname 
const { first, last, twitter } = person;

const arden = {
    first: 'Arden',
    last: 'de Raaij',
    links: {
        social: {
            twitter: 'https://twitter.com/ardennl',
            instagram: 'https://instagram.com/ardennl',
        },
        web: {
            blog: 'https://arden.nl'
        }
    }
};

// Rename value names while destructuring
const { twitter: tweet, instagram: ig } = arden.links.social;

// Example of a settings object; a couple of default values
const settings = { width: 300, color: 'black' }  // height, fontSize
// Adding default values while destructering an object. In this case width and color
// will return what is defined in the settings object, height and fontsize values are
// defined while destructuring
const { width = 100, height = 100, color = 'blue', fontSize = 25} = settings;

以上是关于javascript ES6 - 解构的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript ES6 - 解构赋值

javascript 解构#es6

javascript ES6 - 解构函数

javascript ES6解构

javascript ES6 - 解构阵列

javascript ES6 - 解构