ES6学习笔记

let

let 相比var 来说不能重复声明
let不允许声明之前使用,var声明之前使用是undefined

let 的作用域是块级的,var是全局的。let虽然是块级作用域,但是和c语言不同,c语言在作用域里面找不到就找不到,js找不到会向上级作用域找。

解构赋值

const zhao = {
    name:'赵本山',
    age:'不知道',
    xiaopin:function(){
        console.log('我会演小品')
    }
}   
let {name,age} = zhao;
let {xiaopin} = zhao;
function connect({host="127.0.0.1",username,password,port}){
    console.log(host)
    console.log(username)
    console.log(password)
    console.log(port)
}

connect({
    username:'root',
    password:'root',
    port:33306
})

模版字符串

  1. 反引号字符串中可以直接写换行符

  2. 可以直接解析变量了

let love = `肉肉`
let out = `我喜欢吃${love}`;

对象简化写法

let name = 'aaa';
let age = 16;

let school = {
    name ,
    age,
    improve(){
        console.log('没写键名,默认键名和变量名一致')
    }
}

箭头函数

() => {

}

箭头函数this始终指向声明时的this,而function(){}定义的this指向调用者

for of

for(let i in arr){

}
for(let i of arr){
    
}

for in 里的i是键,for of 里的i是值,

Promise

// 传统写法
step1(function (value1) {
  step2(value1, function(value2) {
    step3(value2, function(value3) {
      step4(value3, function(value4) {
        // ...
      });
    });
  });
});

// Promise 的写法
(new Promise(step1))
  .then(step2)
  .then(step3)
  .then(step4);

模块化

引入import和export语法

async/await

async/await语法可以让异步处理写的跟同步一样。

let response = await fetch('/article/promise-chaining/user.json');
console.log(response);

如果没加await,console.log(response)一定为空。如果加await,会等待结果出来之后再进行下一步。

posted @ 2023-06-15 20:48:29
评论加载中...
发表评论