The most complete Es6 - Es11 (Es8 articles)

There are links to other articles at the end of the article

Es8

1、async/await

async function

An async function is a function declared using the async keyword. An async function is an instance of the AsyncFunction constructor, and the await keyword is allowed within it. The async and await keywords allow us to write Promise-based asynchronous behavior in a more concise way without deliberately chaining promises.
grammar:

async function name([param[, param[, ... param]]]) {
    
    
    //statements  主体
}
await

The await operator is used to wait for a Promise object. It can only be used in async functions.
The await expression will pause the execution of the current async function and wait for the Promise processing to complete. If the Promise is processed normally (fulfilled), the resolve function parameter of its callback is used as the value of the await expression, and the async function continues to be executed. If the Promise handles an exception (rejected), the await expression will throw the Promise's exception reason. In addition, if the value of the expression after the await operator is not a Promise, the value itself is returned.

use:

function resolveAfter2Seconds() {
    
    
  return new Promise(resolve => {
    
    
    setTimeout(() => {
    
    
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
    
    
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: "resolved"
}

asyncCall();

2. Object method extension

Object.values()
The Object.values() method returns an array of all enumerable property values ​​of the given object itself, in the same order as using a for...in loop (the difference is that the for-in loop enumerates the prototype chain properties in the ).

var obj = {
    
     foo: 'bar', baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]

Object.entries()
The Object.entries() method returns an array of key-value pairs for the given object's own enumerable properties.

const object1 = {
    
    
  a: 'somestring',
  b: 42
};
for (const [key, value] of Object.entries(object1)) {
    
    
  console.log(`${
      
      key}: ${
      
      value}`);
}
// output:
// "a: somestring"
// "b: 42"

Object.getOwnPropertyDescriptor()
The Object.getOwnPropertyDescriptor() method returns the property descriptor corresponding to an own property on the specified object. (Own properties refer to properties that are directly assigned to the object and do not need to be looked up from the prototype chain.)
If the specified property exists on the object, its property descriptor object (property descriptor) is returned, otherwise undefined is returned.

const object1 = {
    
    
  property1: 42
};

const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');

console.log(descriptor1.configurable);
// expected output: true

console.log(descriptor1.value);
// expected output: 42

Previous article: The most complete Es6 - Es11 (Es7 articles)
Next article: The most complete Es6 - Es11 (Es9 articles)

Guess you like

Origin blog.csdn.net/weixin_44646763/article/details/124912546