String
object: padStart()
and padEnd()
. The words "Start" and "End" in the function names allude to their role in handling strings. Namely, they allow you to set the parameters for complementing the lines until they reach a specified length. The padStart()
method complements the string from its beginning (left), padEnd()
from the end (right). Lines can be supplemented with specified single characters, other lines, or, by default, spaces. Here is the syntax of the functions in question: str.padStart(targetLength [, padString]) str.padEnd(targetLength [, padString])
targetLength
. It represents the target length of the resulting string. The second parameter, an optional padString
, is a string that will complement the source string. Without this parameter, the space will be used for the addition. If the length of the string to which one of these methods is applied exceeds the specified length, the source string remains unchanged. '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'
Object.values()
method returns an array of the enumerated properties of the object passed to it in the same order in which the forβ¦in
loop does it. The method syntax is extremely simple: Object.values(obj)
obj
parameter is the object whose properties should be obtained. It can be an object or an array (in other words, an object with indices like [ 10, 20, 30] -> { 0: 10, 1: 20, 2: 30 }
). Here is a sample code: const obj = { x: 'xxx', y: 1 }; Object.values(obj); // ['xxx', 1] const obj = ['e', 's', '8']; // , { 0: 'e', 1: 's', 2: '8' }; Object.values(obj); // ['e', 's', '8']
Object.entries()
method returns an array of the object's own enumerated properties in the format [, ]
, in the same order as Object.values()
. The syntax of the method is similar to Object.values()
, and the rest of these methods are similar: Object.entries(obj)
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']]
Object.getOwnPropertyDescriptors()
method returns a handle to the property of the passed object. Own property is defined directly in the object, and not obtained through a chain of prototypes. Here is the syntax of the method: Object.getOwnPropertyDescriptor(obj, prop)
obj
argument is an object, the data on which property must be obtained, the argument prop
is the name of the property whose handle we are interested in. As a result of successful execution of this method, an object will be returned, which can contain the following keys:configurable β true
- if the type of the property descriptor can be changed and if the property can be removed from the containing object, otherwise false
.enumerable
- true
- if the property is available when listing the properties of an object, otherwise - false
.writable
- true
- if the value associated with the property can be changed, otherwise false
(for data descriptors)get
is a function that returns a property value or, if it is missing, undefined
(for access descriptors)set
is a function that changes the value of a property, or if it is absent, undefined
(for access descriptors)value
is the value associated with the property (for data descriptors). const obj = { get es8() { return 888; } }; Object.getOwnPropertyDescriptor(obj, 'es8'); // { // configurable: true, // enumerable: true, // get: function es8(){}, //- // set: undefined // }
[10, 20, 30,]
and { x: 1, }
.SyntaxError
error. Now this is not happening. Here is an example of a function declaration, in the parameter list of which there is a trailing comma: function es8(var1, var2, var3,) { // ... }
es8(10, 20, 30,);
async
allows you to define an asynchronous function that returns an AsyncFunction object. Inside, their work is very similar to how generators work. function fetchTextByPromise() { return new Promise(resolve => { setTimeout(() => { resolve("es8"); }, 2000)); }); } async function sayHello() { const externalFetchedText = await fetchTextByPromise(); console.log(`Hello, ${externalFetchedText}`); // Hello, es8 } sayHello();
sayHello()
will result in the output of the string β Hello, es8
β to the log with a delay of 2 seconds, while the main execution thread is not blocked. Here's what it looks like: console.log(1); sayHello(); console.log(2);
1 // 2 // Hello, es8 // 2
console.log()
calls are executed one by one, and the asynchronous function, without blocking the main thread, is executed after 2 seconds.await
keyword can only be used in functions defined using the async
.Atomics
object cannot be used as a constructor, it is, in this respect, similar to Math
. There are Atomics
object methods that allow you to perform various safe operations on elements of typed arrays that serve to access SharedArrayBuffer
objects. Among them, for example, the Atomics.add()
method for adding a specified number to what is stored in a given array position. There are methods in Atomics
for managing locks, in particular, Atomics.wait()
and Atomics.wake()
. const esth = 8; helper`ES ${esth} is `; function helper(strs, ...keys) { const str1 = strs[0]; // ES const str2 = strs[1]; // is let additionalPart = ''; if (keys[0] == 8) { // 8 additionalPart = 'awesome'; } else { additionalPart = 'good'; } return `${str1} ${keys[0]} ${str2} ${additionalPart}.`; }
esth
contains the number 8
, the function will return β ES 8 is awesome
β. If esth
is 7
, the function will return β ES 7 is good
β.\u
or \x
. In particular, for example, after \u
Unicode character code must follow, otherwise an error message will appear. It is expected that in ES9 this restriction will be removed.Source: https://habr.com/ru/post/332998/
All Articles