Properties of the top-level object

As we all know, the browser global environment refers to the Window and Node refers to the global object. Properties of top-level objects are equivalent to global variables in ES6.

window.a = 1
a / / 1

a = 2
window.a / / 2
Copy the code

As you can see from the above code, assignment of attributes to top-level objects is the same thing as assignment of global variables.

Es6 maintains compatibility for both global variables declared by var and function commands, which are still properties of top-level objects. The let const calss command declares global variables that are not attributes of the top-level object.

var a = 1
window.a / / 1
let b = 2
window.b // undefined
Copy the code

GlobalThis object

The javaScript language has a top-level object that provides the global environment (that is, the global scope) in which all code runs. But top-level objects are not uniform across implementations. At the level of language standards, ES2020 introduces globalThis as a top-level object. This means that globalThis exists in any context and can be retrieved from it to point to the top-level object pointing to this in the global context

Deconstructive assignment of values and Bores

When deconstructing an assignment, if the value and Boolean are to the right of the equals sign, the object is converted first.

let {toString: s} = 123;
s === Number.prototype.toString // true

let {toString: s} = true;
s === Boolean.prototype.toString // true
Copy the code

In the code above, both numeric and Boolean-wrapped objects have toString attributes, so the variable S can take on a value. The rule for deconstructing assignment is to turn the value to the right of the equals sign into an object whenever it is not an object or array. Null and undefined cannot be converted to objects, so destructuring them will report an error.

The label template

The functionality of a template string can also be followed by the name of a function that will be called to process the template string. This is the tag template. A tag template is not a template, a special form of Russian function call. The tag refers to the function, and the template string that follows it is its argument. However, if there are variables in the template string, the template string is processed into multiple arguments before the function is called.

function tag() {
 return 'tag';
}
let name = "Joh";
var res = tag`hello ${name}`;
console.log(res); // tag
Copy the code

The above code, an unprocessed tag function combined with the template string, invalidates the template string. One important use of tag templates is to filter HTML strings to prevent users from entering malicious content

Exponential operator

** represents the exponential operator

2支那2 / / 4
2支那3 / / 8
// A feature of the operator is right-associative. When multiple exponential operators are used together, they are evaluated from the far right.
2支那3支那2 / / 512
let a = 1.5
a **= 2 // Equivalent to a = a * a

let b = 4
b **=3 // the same thing as b = b * b * b
Copy the code

BigInt type

Is a new data type (primitive type) introduced in ES2020. BigInt is only used to represent integers. There is no limit on the number of digits. BigInt must be suffix-n typeof 123n // ‘BigInt’