Can I set a private class field using a variable as identifier? How?

No, this doesn’t look to be possible. From the proposal FAQ: Why doesn’t this[‘#x’] access the private field named #x, given that this.#x does? This would complicate property access semantics. Dynamic access to private fields is contrary to the notion of ‘private’. E.g. this is concerning: class Dict extends null { #data = something_secret; add(key, … Read more

Unable to use Arrow functions inside React component class [duplicate]

It’s not the arrow function that’s causing a problem here. Class properties are not part of the ES6 specification. handleUpdateInput = (value) => { // … }; If you want to be able to transform this code, you’ll need to add the class properties babel plugin. Alternatively, this transform is provided as part of Babel’s … Read more

What are “class fields” in JavaScript?

Simply put, the reason to use this is ease of understanding the code. Without class field declarations, you would do something like: class Person { constructor() { this.firstName = “Mike”; this.lastName = “Patel”; this.getName = () => { return this.firstName + ” ” + this.lastName; }; } } var p = new Person(); console.log(p.firstName); // … Read more

How to know if a function is async?

Theory Native async functions may be identifiable when being converted to strings: asyncFn[Symbol.toStringTag] === ‘AsyncFunction’ Or by AsyncFunction constructor: const AsyncFunction = (async () => {}).constructor; asyncFn instanceof AsyncFunction === true This won’t work with Babel/TypeScript output, because asyncFn is regular function in transpiled code, it is an instance of Function or GeneratorFunction, not AsyncFunction. … Read more