6.對象解構(gòu)

對象解構(gòu)

對象結(jié)構(gòu)能夠幫助我們更為簡便的提取對象中的屬性,對其重命名,以及賦予默認值。

const Tom = {
    name: 'Tom jones',
    age: 25,
    family: {
        mother: 'Norah Jones',
        father: 'Rachard Jones',
        brother: 'Howard Jones',
    }
}

const father = 'Dad';

const { name, age } = Tom;
const { mother, father: f, brother: b, sister = 'Marry' } = Tom.family;

console.log(name); // Tom jones
console.log(f); // Howard jones
console.log(father); // Dad
// console.log(brother); // brother is not defined
console.log(sister); // Marry

默認設(shè)置小例子

function appendChildDiv(options = {}) {
    const {
        parent = 'body',
        width = '100px',
        height = '80px',
        backgroundColor = 'green',
    } = options;

    const div = document.createElement('div');
    div.style.width = width;
    div.style.height = height;
    div.style.backgroundColor = backgroundColor;

    document.querySelector(parent).appendChild(div);
}

appendChildDiv({ width: '200px', height: '300px', backgroundColor: 'red' });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評論 25 708
  • 序 從最近的js入門系列的閱讀量逐步遞減,觀眾老爺?shù)呐d趣也不再能夠接受一些細節(jié)性的地方深度挖掘,讓我有了一些思考。...
    zhaolion閱讀 1,736評論 5 19
  • 每周經(jīng)典回顧——你最喜歡的是哪篇 1.你是什么樣的人 2.同時出售兩份時間 3.任何關(guān)系中到最后都是在自我修行 4...
    德合閱讀 651評論 0 0
  • 一、MacOS自帶ruby為2.0.0版本,如果你想裝最新版的Rails需要更新到ruby2.2.4版本.更新系統(tǒng)...
    JackYao閱讀 1,930評論 0 49

友情鏈接更多精彩內(nèi)容