This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Feature profile

  • Declare class fields: Class fields can be defined and initialized at the top level of the class
  • Private methods & Fields: Use the # prefix to define the class’s private methods and fields
  • Class static public methods and fields: added static public fields, static private methods, and static private fields
  • ECMScript Class static initialization block: Evaluates a static initialization code block during class declaration/definition, with access to a class’s private fields
  • Detect private fields: You can use the IN operator to return true if the specified property/field is in the specified object/class, and also to determine private fields
  • Regular match index: This proposal provides a new/dFlag to obtain additional information about the start and end of each match and index position in the input string
  • Add.at() methods to all built-in indexable data
  • Object.hasown (Object, property) : Use object.hasown instead
  • Object.prototype.hasOwnProperty.call
  • Error Cause: Indicates the Cause of an Error for easy transmission

The following highlights relevant development uses:

class Test { static instance = new Test(0); Static #value=123; Static #print(){return this.#value; }}Copy the code

.at

Add the.at() method to all basic indexable classes, currently in Stage 3. In many cases, a negative index like the one in Python can be very useful. For example, in Python we can access the last element of an array through arr[-1], rather than the current JavaScript way of accessing arr[arr.length-1]. The negative number here is used as a reverse index from the starting element (arr[0]).

But the problem with JavaScript today is that the [] syntax isn’t just used in arrays (it certainly isn’t in Python either), and arrays aren’t just used as indexes. Referring to a value by index, as arr[1] does, actually refers to the “1” attribute of the object. So arr[-1] is perfectly usable in today’s JavaScript engines, but it probably doesn’t mean what we mean: it refers to the “-1” property of the target object, rather than a reverse index.

This proposal provides a generic way to access any backward or forward-indexed element through.at methods on any indexable type (Array, String, and TypedArray).

Example:

Const arr = [1, 2]; arr.at(-1); // 2 arr.at(-2); / / 1Copy the code

Object.hasOwn()

A way to make more accessible in alternative Object. The prototype. The hasOwnProperty (), is at a Stage 4 stages.

In fact we can now through the Object. The prototype. HasOwnProperty using proposal contains features. However, it is not safe to use obj. HasOwnProperty (‘foo’) directly from the object’s own hasOwnProperty, because this obJ may override the definition of hasOwnProperty, and MDN warns against this use.