6.对象解构

对象解构

对象结构能够帮助我们更为简便的提取对象中的属性,对其重命名,以及赋予默认值。

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

默认设置小例子

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' });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,408评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,475评论 25 709
  • 序 从最近的js入门系列的阅读量逐步递减,观众老爷的兴趣也不再能够接受一些细节性的地方深度挖掘,让我有了一些思考。...
    zhaolion阅读 5,532评论 5 19
  • 每周经典回顾——你最喜欢的是哪篇 1.你是什么样的人 2.同时出售两份时间 3.任何关系中到最后都是在自我修行 4...
    德合阅读 3,602评论 0 0
  • 一、MacOS自带ruby为2.0.0版本,如果你想装最新版的Rails需要更新到ruby2.2.4版本.更新系统...
    JackYao阅读 5,800评论 0 49