ecmascript-next

What are “class fields” in JavaScript?

て烟熏妆下的殇ゞ 提交于 2020-05-23 10:52:11
问题 I was reading about JavaScript classes, and came across this term " public class fields syntax ". On digging a bit deeper into it I came across this Babel's documentation on class properties. Can someone please explain - implementation-wise what are the use-cases for this new syntax? (What solutions/benefits does it offer to JavaScript, which were missing so far?) Here's an example below (ran without errors in Google Chrome) : class Person { firstName = "Mike"; lastName = "Patel"; // this is

Why can't String.raw end with a backslash?

混江龙づ霸主 提交于 2020-05-15 05:25:31
问题 String.raw can be used to create a string that contains backslashes, without having to double up those backslashes. Historically, you'd need to double up backslashes when creating a string: let str = "C:\\Program Files\\7-Zip"; console.log(str); String.raw allows your code to show the path without doubled backslashes: let str = String.raw`C:\Program Files\7-Zip`; console.log(str); The above code works fine, but today I discovered that it doesn't work if the raw string ends with a backslash:

Is it possible to combine optional chaining with arrays and map (in Javascript)?

二次信任 提交于 2020-04-30 08:27:33
问题 I recently learned about optional chaining in Javascript and have been making use of it in a React/NodeJS project. Works great. I noticed I have been using it with arrays map , even without thinking about it much -- it seemed a natural use (here items is an array, or possibly undefined ) {items?.map(postListItem => .... That is, it will map if items exists, but not if items is undefined , but would avoid any run-time errors if I were to call map on undefined Nonetheless I don't know if this

Is it possible to combine optional chaining with arrays and map (in Javascript)?

荒凉一梦 提交于 2020-04-30 08:22:06
问题 I recently learned about optional chaining in Javascript and have been making use of it in a React/NodeJS project. Works great. I noticed I have been using it with arrays map , even without thinking about it much -- it seemed a natural use (here items is an array, or possibly undefined ) {items?.map(postListItem => .... That is, it will map if items exists, but not if items is undefined , but would avoid any run-time errors if I were to call map on undefined Nonetheless I don't know if this

What does the operation HostPromiseRejectionTracker do?

和自甴很熟 提交于 2020-03-25 21:30:33
问题 I looked at HostPromiseRejectionTracker in the ECMAScript specification, but still did not understand what it was doing. It does not have specific steps of the algorithm, so it is not clear how this operation works in the current code. One thing is clear that HostPromiseRejectionTracker is called when creating a new Promise when executing a function that calls the reject function. And the second time when "then" method is called for the first time, HostPromiseRejectionTracker is called only

JavaScript Decorator on Class constructor

我是研究僧i 提交于 2020-02-04 04:14:28
问题 I'm trying to add some properties in class instances (like a Plugin system). For that, I followed this example to do that with a Class Decorator: function testDecorator(target:any) { // save a reference to the original constructor var original = target; // the new constructor behaviour var f : any = function (...args: any[]) { console.log("New: " + original.name); return original.apply(this, args) } // copy prototype so intanceof operator still works f.prototype = original.prototype; //

Javascript class methods versus properties

点点圈 提交于 2020-01-31 03:39:26
问题 I've seen code using Javascript classes use the following form (example is React): class UserProfile extends Component { state = { open: false } handleOpen = () => { this.setState({ open: true }) } } Why is handleOpen implemented as a property which is set to a function instead of something like: class UserProfile extends Component { state = { open: false } handleOpen() { this.setState({ open: true }) } } Thanks in advance! 回答1: That's also a function, but it's called an arrow function and

Javascript class methods versus properties

霸气de小男生 提交于 2020-01-31 03:39:07
问题 I've seen code using Javascript classes use the following form (example is React): class UserProfile extends Component { state = { open: false } handleOpen = () => { this.setState({ open: true }) } } Why is handleOpen implemented as a property which is set to a function instead of something like: class UserProfile extends Component { state = { open: false } handleOpen() { this.setState({ open: true }) } } Thanks in advance! 回答1: That's also a function, but it's called an arrow function and

Arrow vs classic method in ES6 class

情到浓时终转凉″ 提交于 2020-01-26 20:13:52
问题 Is there any reason to write classic syntax of ES6 methods? class MyClass { myMethod() { this.myVariable++; } } When I use myMethod() as callback on some event, I must write something like this (in JSX): // Anonymous function. onClick={() => { this.myMethod(); }} // Or bind this. onClick={this.myMethod.bind(this)} But if I declare method as arrow function: class MyClass { myMethod = () => { this.myVariable++; } } than I can write just (in JSX): onClick={this.myMethod} 回答1: The feature you are

using spread operator in typescript

旧城冷巷雨未停 提交于 2020-01-24 13:29:40
问题 I want to write a function which returns me a component wrapped up in another. The function I'm trying to write is like below in JavaScript. function GetGroup({ name, text, isRequired, ...props }) Here, name , text , and isRequired is obtained from the passed arguments and others are sent to another component as props . How to write it in TypeScript? 回答1: So firstly, Object Rest/Spread is a proposed ECMAScript feature that is well on its way to being standardized, having reached Stage 4, and