发布于 2026-01-06 0 阅读
0

JavaScript ES6

JavaScript ES6

JavaScript 由 Brendan Eich 于 1995 年发明,并于 1997 年成为 ECMA 标准。

ECMAScript是该语言的正式名称。

ECMAScript 版本已被缩写为 ES1、ES2、ES3、ES5 和 ES6。

自 2016 年起,新版本按年份命名(ECMAScript 2016 / 2017 / 2018)。

箭头函数

const sum =(a,b)=> a+b
console.log(sum(2,6))
//prints 8
Enter fullscreen mode Exit fullscreen mode

默认参数

function print(a=5){
console.log(a)
}
print();
//prints 5
Enter fullscreen mode Exit fullscreen mode

让范围

let a=3;
if(true){
let a=5;
console.log(a);//prints 5
}
console.log(a);//prints 3
Enter fullscreen mode Exit fullscreen mode

康斯特

//can be assigned only once
var x = 50;
// Here x is 50
{
  const x = 16;
  console.log(x);// Here x is 16
}
console.log(x); // Here x is 50
Enter fullscreen mode Exit fullscreen mode

多行字符串

console.log(`This is a 
multiline string`);
Enter fullscreen mode Exit fullscreen mode

模板字符串

const name ='Chhetri'
const message =`Buddhadeb ${name}`
console.log(message)
//Prints Buddhadeb Chhetri
Enter fullscreen mode Exit fullscreen mode

指数运算符

const byte =2 ** 8
console.log(byte)
//same as : Math.pow(2,8)
Enter fullscreen mode Exit fullscreen mode

价差操作商

const a=[1,2]
const b=[3,4]
const c=[...a,...b]
console.log(c) 
//[1,2,3,4]
Enter fullscreen mode Exit fullscreen mode

字符串包含()

console.log('apple'.includes('p'))
//Prints true
console.log('apple'.includes('tt'))
//prints false
Enter fullscreen mode Exit fullscreen mode

字符串以()开头

console.log('ab'.repeat(3))
//prints 'ababab'
Enter fullscreen mode Exit fullscreen mode

解构数组

let [a,b] =[3,7];
console.log(a);//3
console.log(b);//7

Enter fullscreen mode Exit fullscreen mode

解构对象

let obj ={
a:55,
b:44
};
let{a,b}= obj;
console.log(a);
//55
console.log(b);
//44
Enter fullscreen mode Exit fullscreen mode

对象属性赋值

const a=2
const b=5
const obj={a,b}
//before es6:
//obj ={a:a,b:b}
console.log(obj)
//{a:2,b:5}
Enter fullscreen mode Exit fullscreen mode

Object.Assign()

const obj1 ={a:1}
const obj2 ={b:2}
const obj3 =Object.assign({},obj1,obj2)
console.log(obj3)
//{a:1 ,b:2}
Enter fullscreen mode Exit fullscreen mode

最终的承诺

promise
.then((result) => {...})
.catch((error) => {...})
.finally(() => { 
    //Logic independent of success/error 
})
/* The handeler is called when the promise is fulfilled or rejected.*/
Enter fullscreen mode Exit fullscreen mode
文章来源:https://dev.to/buddhadebchhetri/javascript-es6-ilj