重新回头学习归纳ES6的知识点---------解构赋值

news/2024/7/11 1:28:59 标签: es6, javascript

解构赋值

一 .对象解构

    【 基本 】

javascript">let node = {
    type: "Identifier",
    name: "foo"
};
let { type, name } = node;
console.log(type); // "Identifier"
console.log(name); // "foo"

    【 解构赋值 】

let node = {
    type: "Identifier",
    name: "foo"
},
type = "Literal",
name = 5;
// 使用解构来分配不同的值
({ type, name } = node);    //花括号不能在表达式左侧因为会被当作块级作用域执行导致语法出错,所以括号括起来
console.log(type); // "Identifier"
console.log(name); // "foo"

function outputInfo(value) {
    console.log(value === node); // true
}
outputInfo({ type, name } = node);

    【[ 默认值 】

let node = {
    type: "Identifier",
    name: "foo"
};
let { type, name, value = true } = node;
console.log(type); // "Identifier"
console.log(name); // "foo"
console.log(value); // true

【 为非同名局部变量赋值 】

let node = {
    type: "Identifier"
};
let { type: localType, name: localName = "bar" } = node;
console.log(localType); // "Identifier"
console.log(localName); // "bar"

【 嵌套对象解构 】

let node = {
    type: "Identifier",
    name: "foo",
    loc: {
        start: {
            line: 1,
            column: 1
        },
    end: {
        line: 1,
        column: 4
    }
}
};
let { loc: { start }} = node;
let { loc: { start: start2 }} = node;
console.log(start.line); // 1
console.log(start2); // 1

 


http://www.niftyadmin.cn/n/1496750.html

相关文章

mysql5.7 修改用户密码

修改vi /etc/my.cnf,增加skip-grant-tables可以免密码登录mysql use mysql ; update user set authentication_stringPASSWORD("123456") where userroot; 再次修改vi /etc/my.cnf,注释skip-grant-tables,重启mysql mysql -uroot -…

重新回头学习归纳ES6的知识点-------数组循环

1.forEach循环 就是代替普通的for循环,它接受两个参数(循环回调函数,this的指向) arr.forEach(function(val,index,arr){console.log(val,index,arr); },this); 2. map 正常情况下,需要配合return&#xff0…

551 Student Attendance Record I 学生出勤纪录 I

给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: A : Absent,缺勤 L : Late,迟到 P : Present,到场如果一个学生的出勤纪录中不超过一个A(缺勤)并且不超过两个连续的L(迟到),那么这个学…

Java线程池相关类-Executor框架

1.Executor 接口源码&#xff1a; public interface Executor {/*** Executes the given command at some time in the future. The command* may execute in a new thread, in a pooled thread, or in the calling* thread, at the discretion of the <tt>Executor<…

重新回头学习归纳ES6的知识点-------promise(承诺)

概述&#xff1a; 是异步编程的一种解决方案。解决异步回调问题。 从语法上说&#xff0c;Promise 是一个对象&#xff0c;从它可以获取异步操作的消息。 特点&#xff1a; 对象的状态不受外界影响。 Promise对象代表一个异步操作&#xff0c;有三种状态&#xff1a;pending&…

Java多线程 5:Thread中的实例方法

Thread类中的方法调用方式&#xff1a;快速到底 学习 Thread 类中的方法是学习多线程的第一步。在学习多线程之前特别提出一点&#xff0c;调用 Thread 中的方法的时候&#xff0c;在线程类中&#xff08;千万别忘记了这个前提条件&#xff09;&#xff0c;有两种方式&#xff…

重新回头学习归纳ES6的知识点---------模块 Module 的语法

1.概述&#xff1a; ES6 模块不是对象&#xff0c;而是通过export命令显式指定输出的代码&#xff0c;再通过import命令输入。 import命令会被 JavaScript 引擎静态分析&#xff0c;先于模块内的其他语句执行&#xff08;import命令叫做“连接” binding 其实更合适&#xff…

7.Web Service 调用天气代码

1. 1 2500多个城市天气预报 WEB服务公用事业2 Endpoint:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx3 Disco:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?disco4 WSDL:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl5 …