ecmascript-next

Decorator feature not working (unexpected token)

六眼飞鱼酱① 提交于 2021-01-28 12:57:30
问题 Just tried to use decorators in React: import React from 'react'; import Fade from './Transitions/Fade' import withVisible from './withVisible' @withVisible() const App = props => <Fade visible={props.visible} duration={500}> Hello </Fade> export default App If I use the common way ( withVisible()(App) ) then it's working properly. (My guess is that NodeJS can't compile my code with the @ ) [Syntax error: Unexpected token (@) ] import React from 'react' const withVisible = () => Component =>

What's difference between two ways of defining method on React Class in ES6

两盒软妹~` 提交于 2020-12-31 05:20:30
问题 I see this a lot in ES6 React code class Foo extends React.Component { bar = () => { console.log("bar") } baz() { console.log("baz") } } Seems like they both define methods bar and baz on Foo but how are they different. 回答1: The declarations differ in how the function are written and the context of this , In the first syntax bar = () => { console.log("bar") } the function is written using Arrow function syntax. An arrow function does not have its own this ; the this value of the enclosing

How to convert BigInt to Number in JavaScript?

倾然丶 夕夏残阳落幕 提交于 2020-12-02 03:24:50
问题 I found myself in the situation where I wanted to convert a BigInt value to a Number value. Knowing that my value is a safe integer, how can I convert it? 回答1: Turns out it's as easy as passing it to the Number constructor: const myBigInt = BigInt(10); // of course, `10n` also works const myNumber = Number(myBigInt); 回答2: You can use parseInt or Number const large = BigInt(309); const b = parseInt(large); console.log(b); const n = Number(large); console.log(n); 来源: https://stackoverflow.com

transpiler battle: breaking out of nested function, with vs without throw

若如初见. 提交于 2020-08-26 13:54:08
问题 I have just finished writing "version 0" of my first (toy) transpiler. It works. It turns a string of "pseudo JavaScript" (JavaScript with an additional feature) into a string of runnable JavaScript. Now, I want to improve it. The work area possibly most interesting for other SO users is this: The compiled code (i.e., output of my transpiler) does not heed a coding style recommendation as given in an accepted answer to some earlier SO question. If I would have at my hands a second transpiler

transpiler battle: breaking out of nested function, with vs without throw

橙三吉。 提交于 2020-08-26 13:54:05
问题 I have just finished writing "version 0" of my first (toy) transpiler. It works. It turns a string of "pseudo JavaScript" (JavaScript with an additional feature) into a string of runnable JavaScript. Now, I want to improve it. The work area possibly most interesting for other SO users is this: The compiled code (i.e., output of my transpiler) does not heed a coding style recommendation as given in an accepted answer to some earlier SO question. If I would have at my hands a second transpiler

ECMAScript object spread/rest - assigning to multiple properties at once

你离开我真会死。 提交于 2020-07-15 09:29:28
问题 The new object rest/spread syntax has some surprisingly nice applications, like omitting a field from an object. Is there a (proposed) way to also assign to several properties of an object, the values from variables with the same names? In other words, a shorter way to say: o.foo = foo; o.bar = bar; o.baz = baz; Note: Without losing the existing properties of o , only adding to them. 回答1: Use Object.assign : const o = { initial: 'initial' }; const foo = 'foo'; const bar = 'bar'; const baz =

ECMAScript object spread/rest - assigning to multiple properties at once

孤人 提交于 2020-07-15 09:26:17
问题 The new object rest/spread syntax has some surprisingly nice applications, like omitting a field from an object. Is there a (proposed) way to also assign to several properties of an object, the values from variables with the same names? In other words, a shorter way to say: o.foo = foo; o.bar = bar; o.baz = baz; Note: Without losing the existing properties of o , only adding to them. 回答1: Use Object.assign : const o = { initial: 'initial' }; const foo = 'foo'; const bar = 'bar'; const baz =

Can I create an object for which Array.isArray() returns true without using the Array constructor or array literal?

醉酒当歌 提交于 2020-07-03 06:17:28
问题 I can easily make a plain object look like an array by setting its prototype to Array.prototype : const obj = {}; Reflect.setPrototypeOf(obj, Array.prototype); (I'm aware that there are also some problems with the magic length property and sparse arrays, but that's not the point of this question.) I want to make Array.isArray(obj) return true (of course without modifing the Array.isArray() method). The MDN polyfill for Array.isArray() is as follows: if (!Array.isArray) { Array.isArray =

Succinct/concise syntax for 'optional' object keys in ES6/ES7?

假装没事ソ 提交于 2020-06-17 09:50:51
问题 There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript: const obj = { requiredKey1: ..., requiredKey2: ... }; if (someCondition) { obj.optionalKey1 = ...; } Is there a way to define the object all at once with both optional and required keys? 回答1: You can use object spread to have an optional property. Note: Object Rest/Spread is a stage 4 proposal for ECMAScript. You might need the babel transform to use it.

Succinct/concise syntax for 'optional' object keys in ES6/ES7?

此生再无相见时 提交于 2020-06-17 09:50:07
问题 There are already a lot of cool features in ES6/ES7 for defining Javascript objects. However, the following pattern is common in Javascript: const obj = { requiredKey1: ..., requiredKey2: ... }; if (someCondition) { obj.optionalKey1 = ...; } Is there a way to define the object all at once with both optional and required keys? 回答1: You can use object spread to have an optional property. Note: Object Rest/Spread is a stage 4 proposal for ECMAScript. You might need the babel transform to use it.