Table of Contents
String filling
Object value traversal
Get the property descriptors of the object
Trailing commas in function parameter lists and calls
Asynchronous function
Shared Memory and atomic operations
Home Web Front-end JS Tutorial Overview of the latest ES8 features

Overview of the latest ES8 features

Jul 21, 2017 am 09:59 AM
ecmascript Overview

ECMAScript 2017 (ES8) feature overview compiled from ES8 was Released and here are its Main New Features, summarized in the author's Modern JavaScript Development: Grammar Basics and Practical Skills series of articles; you are also welcome to pay attention to the front-end weekly checklist series to get first-hand Information.

ECMAScript 2017 or ES8 was officially released by TC39 at the end of June 2017. You can browse the complete version here; the representative features of ES8 include string padding, Object value traversal, object attribute descriptor acquisition, function parameter list and trailing commas in calls, asynchronous functions, shared memory and atomic operations, etc.

String filling

ES8 has added built-in string filling functions, namely padStart and padEnd. This function can ensure that the string reaches a fixed size by filling the head or tail of the string. Length; developers can specify the padded string or use default spaces. The function is declared as follows:

str.padStart(targetLength [, padString])

str.padEnd(targetLength [, padString])
Copy after login

As shown above, the first parameter of the function is the target length, which is the final generated The string length; the second parameter is the specified padding string:

'es8'.padStart(2);          // 'es8'
'es8'.padStart(5);          // '  es8'
'es8'.padStart(6, 'woof');  // 'wooes8'
'es8'.padStart(14, 'wow');  // 'wowwowwowwoes8'
'es8'.padStart(7, '0');     // '0000es8'
'es8'.padEnd(2);          // 'es8'
'es8'.padEnd(5);          // 'es8  '
'es8'.padEnd(6, 'woof');  // 'es8woo'
'es8'.padEnd(14, 'wow');  // 'es8wowwowwowwo'
'es8'.padEnd(7, '6');     // 'es86666'
Copy after login

Object value traversal

Object.values ​​function will return the enumerable The attribute value array of the example, the order of the values ​​in the array is consistent with the for-in loop, the function declaration is:

Object.values(obj)
Copy after login

The first parameter obj is the target object that needs to be traversed, it can is an object or array (an array can be regarded as an object whose keys are subscripts):

const obj = { x: 'xxx', y: 1 };
Object.values(obj); // ['xxx', 1]

const obj = ['e', 's', '8']; // same as { 0: 'e', 1: 's', 2: '8' };
Object.values(obj); // ['e', 's', '8']

// when we use numeric keys, the values returned in a numerical 
// order according to the keys
const obj = { 10: 'xxx', 1: 'yyy', 3: 'zzz' };
Object.values(obj); // ['yyy', 'zzz', 'xxx']
Object.values('es8'); // ['e', 's', '8']
Copy after login

The Object.entries method will Enumeration properties and values ​​are returned in the form of a two-dimensional array. The order in the array is consistent with Object.values. The declaration and use of this function are:

const obj = { x: 'xxx', y: 1 };
Object.entries(obj); // [['x', 'xxx'], ['y', 1]]

const obj = ['e', 's', '8'];
Object.entries(obj); // [['0', 'e'], ['1', 's'], ['2', '8']]

const obj = { 10: 'xxx', 1: 'yyy', 3: 'zzz' };
Object.entries(obj); // [['1', 'yyy'], ['3', 'zzz'], ['10': 'xxx']]
Object.entries('es8'); // [['0', 'e'], ['1', 's'], ['2', '8']]
Copy after login

Get the property descriptors of the object

The getOwnPropertyDescriptors function will return the descriptor of a specified property of the specified object; the property must be defined by the object itself rather than inherited from the prototype chain. The declaration of the function is:

Object.getOwnPropertyDescriptor(obj, prop)
Copy after login

obj is the source object, and prop is the property name that needs to be viewed; the keys included in the result may include configurable, enumerable, writable, get, set, and value.

const obj = { get es8() { return 888; } };
Object.getOwnPropertyDescriptor(obj, 'es8');
// {
//   configurable: true,
//   enumerable: true,
//   get: function es8(){}, //the getter function
//   set: undefined
// }
Copy after login

Trailing commas in function parameter lists and calls

This feature allows us to add trailing commas when defining or calling functions without reporting an error :

function es8(var1, var2, var3,) {
  // ...
}
es8(10, 20, 30,);
Copy after login

Asynchronous function

ES8 allows the use of async/await syntax to define and execute asynchronous functions. The async keyword will return a certain AsyncFunction object; in the internal implementation, although the implementation principles of asynchronous functions and iterators are similar, they will not be converted into iterator functions:

function fetchTextByPromise() {
  return new Promise(resolve => { 
    setTimeout(() => { 
      resolve("es8");
    }, 2000);
  });
}
async function sayHello() { 
  const externalFetchedText = await fetchTextByPromise();
  console.log(`Hello, ${externalFetchedText}`); // Hello, es8
}
sayHello();

console.log(1);
sayHello();
console.log(2);

// 调用结果
1 // immediately
2 // immediately
Hello, es8 // after 2 seconds
Copy after login

Shared Memory and atomic operations

Shared memory allows multiple threads to read and write data concurrently, while atomic operations can perform concurrency control to ensure the sequential execution of multiple competing threads. This section introduces the new constructor SharedArrayBuffer and the namespace object Atomics that contains static methods. The Atomic object is similar to Math. We cannot create its instance directly, but can only use the static methods it provides:

  • add /sub - Add or subtract a value at a certain position

  • and / or /xor - Perform bit operations

  • load - Get the value

##

The above is the detailed content of Overview of the latest ES8 features. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is es2017 es6 or es8? Is es2017 es6 or es8? Oct 27, 2022 pm 05:37 PM

es2017 is es8. The full name of es is "ECMAScript", which is a universal scripting language implemented according to the ECMA-262 standard. The version officially released in June 2017 is officially called ECMAScript2017 (ES2017). Because it is the 8th version of ECMAScript, it can Referred to as es8.

Overview of operator overloading problems and solutions in C++ Overview of operator overloading problems and solutions in C++ Oct 08, 2023 pm 01:02 PM

Overview of Operator Overloading Problems and Solutions in C++ Introduction: Operator overloading is an important feature of the C++ language, which allows programmers to customize existing operators to operate custom data types. However, operator overloading needs to be used with caution, because if used improperly or excessively, it will lead to problems such as reduced code readability, ambiguity, and reduced efficiency. This article will outline common problems with operator overloading in C++ and provide corresponding solutions and code examples. 1. Problems with operator overloading 1.1 Ambiguity problem in operator overloading

Python Tkinter shows off its power: Create stunning GUI applications Python Tkinter shows off its power: Create stunning GUI applications Mar 24, 2024 am 09:11 AM

Tkinter is a powerful GUI library in python that can be used to create cross-platform desktop applications. With its ease of use and wide range of features, it provides various tools for building user interfaces, handling events and managing layouts. Creating a GUI Window To create a GUI window, you need to use the Tkinter.Tk() method. This method returns a Tk() object that represents the application's main window. A window can have a title using the title() method, and the window size and position using the geometry() method. importtkinterastkroot=tk.Tk()root.title("My first Tkinter application")root.g

A quick and detailed explanation of all the features of ES6~ES12 in one article! A quick and detailed explanation of all the features of ES6~ES12 in one article! Jul 22, 2022 am 11:06 AM

This article will sort out and share with you the features of ECMAScript. It will take you an hour to quickly understand all the features of ES6~ES12. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

PHP High Availability: Best Practices for 24/7 Application Availability PHP High Availability: Best Practices for 24/7 Application Availability Mar 26, 2024 pm 09:31 PM

Achieving high availability of applications is critical to ensure seamless operation of critical business services. For applications built with PHP, there are several best practices that can be used to achieve 24/7 availability. Failover and fault-tolerant load balancing: Use a backend load balancer to distribute traffic to multiple servers to avoid single points of failure. Failover: Configure an automatic failover mechanism to transfer traffic to an alternate server in the event of a failure. Fault-tolerant encoding: Use fault-tolerant encoding techniques, such as RaiD or erasure codes, to protect data from disk failures. Redundant and elastic auto-scaling: Enable auto-scaling to dynamically add or remove servers based on load. Multi-AZ deployment: Deploy applications to multiple Availability Zones (AZ) to maximize

Overview of EventLoop library in PHP8.0 Overview of EventLoop library in PHP8.0 May 14, 2023 am 08:49 AM

With the development of the PHP language, developers need more tools to solve the needs and challenges of modern applications, one of which is event-driven programming, and the EventLoop library of PHP8.0 was born for this purpose. This article will provide an overview and introduction to the library. What is EventLoop In traditional PHP applications, most operations are synchronous. In other words, the program will execute some code, then wait for the relevant data to return, and then continue to execute subsequent code. This programming model is useful for some applications

Revealing the behind-the-scenes story of the Yii framework: Controlling a new realm of PHP development Revealing the behind-the-scenes story of the Yii framework: Controlling a new realm of PHP development Mar 26, 2024 am 10:31 AM

The Yii framework is a modern, high-performance PHP framework designed to simplify and accelerate WEB application development. It provides a robust foundation that enables developers to focus on business logic rather than low-level details. Behind the Scenes Modular Architecture: Yii adopts a modular architecture so that applications can be easily extended and customized. A module is an independent, reusable block of code that can be used to implement a specific functionality, such as user management or e-commerce. MVC Pattern: Yii follows the mvc (Model-View-Controller) pattern, which separates the application logic from the presentation layer. This promotes code maintainability and improves application testability. ORM support: Yii provides a powerful object-relational mapping (ORM) layer that enables developers to

Introducing Wireless Networks Introducing Wireless Networks Feb 19, 2024 pm 10:38 PM

Overview of Wireless Networks With the rapid development of technology, wireless networks have become an indispensable part of modern life. Our mobile phones, computers, smart homes and other devices all rely on wireless networks for communication and connection. In this article, we will provide an overview of wireless networks and discuss its development, principles, and applications. The development of wireless networks can be traced back to radio communication technology in the 19th century. At that time, people used radio waves to realize long-distance sound and image transmission, pioneering wireless communication. With the further development of electronic technology

See all articles