💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## ES7和ES8语法 ### ES7 > 1.includes() > 2.幂运算符( **) ### ES8 > 1.padStart(),padEnd() > 2.Object.values和Object.entries > 3.Object.getOwnPropertyDescriptors > 4.函数参数列表和调用中的尾逗号 > 5.异步函数(Async Functions) ### includes ~~~ var array1 = [1, 2, 3]; console.log(array1.includes(2)); // expected output: true var pets = ['cat', 'dog', 'bat']; console.log(pets.includes('cat')); // expected output: true console.log(pets.includes('at')); // expected output: false ~~~ ### 幂运算 ~~~ let a = 7 ** 12 let b = 2 ** 7 console.log(a === Math.pow(7,12)) // true console.log(b === Math.pow(2,7)) // true ~~~ ### padStart(),padEnd() ~~~ 'x'.padStart(5, 'ab') // 'ababx' 'x'.padStart(4, 'ab') // 'abax' 'x'.padEnd(5, 'ab') // 'xabab' 'x'.padEnd(4, 'ab') // 'xaba' ~~~ 如果原字符串的长度,等于或大于指定的最小长度,则返回原字符串。 ~~~ 'xxx'.padStart(2, 'ab') // 'xxx' 'xxx'.padEnd(2, 'ab') // 'xxx' ~~~ ### Object.values和Object.entries ~~~ const obj = { foo: 'bar', baz: 42 }; Object.values(obj) // ["bar", 42] ~~~ ~~~ const obj = { foo: 'bar', baz: 42 }; Object.entries(obj) // [ ["foo", "bar"], ["baz", 42] ] ~~~ ### Object.getOwnPropertyDescriptors ~~~ const obj = { foo: 123, get bar() { return 'abc' } }; Object.getOwnPropertyDescriptors(obj) // { foo: // { value: 123, // writable: true, // enumerable: true, // configurable: true }, // bar: // { get: [Function: get bar], // set: undefined, // enumerable: true, // configurable: true } } ~~~ ### 函数参数列表和调用中的尾逗号 ~~~ function clownsEverywhere( param1, param2, ) { /* ... */ } clownsEverywhere( 'foo', 'bar', ); ~~~ ### 异步函数(Async Functions) async 函数是什么?一句话,它就是 Generator 函数的语法糖 ~~~ var timer = new Promise((resolve,reject) => { setTimeout(() => { resolve(123) },1000) }) function* gen(){ var f = yield timer } var g = gen(); g.next().value.then((x) => { console.log(x) }) ~~~ ~~~ var f = new Promise((resolve,reject) => { setTimeout(() => { resolve(123) }, 1000) }) f.then((s) => { console.log(s) }) ~~~ ~~~ var f = new Promise((resolve,reject) => { setTimeout(() => { resolve(123) }, 1000) }) async function test() { let test = await f console.log(test) } test() ~~~