ecmascript-7

Circular dependencies in ES6/7

徘徊边缘 提交于 2019-12-08 15:26:56
问题 I was surprised to find that in Babel, I could have two modules import each other without any issues. I have found a few places that refer to this as a known and expected behaviour in Babel. I know that this is widely considered an anti-pattern by a lot of (I'm guessing most) people, but please ignore that for this question: Does anyone know if this is (or will be) correct behaviour in ES6/7? The closest thing I can find to an official answer (and technical explanation) is this comment on

How to support ES7 in ESLint

廉价感情. 提交于 2019-12-08 14:58:30
问题 I have a project setup with WebPack to use ESLint and I'm wanting to use ES7 for the inline bind operator :: . Currently I'm getting the parse errors shown below /Users/ryanvice/Documents/Code/pluralsight-redux-starter/src/components/project/ProjectsPage.js (1/0) ✖ 7:27 Parsing error: Unexpected token : /Users/ryanvice/Documents/Code/pluralsight-redux-starter/src/routes.js (2/2) ✖ 6:26 Parse errors in imported module './components/project/ProjectsPage': Unexpected token : (7:27) import

Destructure array to object property keys

北城余情 提交于 2019-12-05 10:40:22
问题 I have an array of values like: const arr = [1,2,3]; Is there any way I can use destructuring to create the following output? If not, what is the easiest way I can do this in ES6 (or later)? const obj = { one: 1, two: 2, three: 3 }; I tried this, but I guess it doesn't work as this is the syntax for computed keys: const arr = [1,2,3]; const obj = { [one, two, three] = arr }; 回答1: I don't believe there's any structuring/destructuring solution to doing that in a single step, no. I wanted

Error using ES7 async/await with node, webpack and babel-loader

老子叫甜甜 提交于 2019-12-05 10:22:16
I'm trying to use javascript ES7 syntax on the server using node.js with webpack and babel-loader (es2015 + stage-0 presets). I've gotten it to work with babel-node but when I run webpack I get the following error at the async keyword (9:22 is after the async keyword): ERROR in ./src/server.js Module parse failed: C:\dev\node-async-sample\src\server.js Unexpected token (9:22) You may need an appropriate loader to handle this file type. SyntaxError: Unexpected token (9:22) I've put the code on github at https://github.com/qubitron/node-async-sample , any ideas on how to get this to work? Here

How to match only those numbers which have an even number of `%`s preceding them?

落花浮王杯 提交于 2019-12-05 07:26:09
I want to catch numbers appearing anywhere in a string, and replace them with "(.+)". But I want to catch only those numbers which have an even number of % s preceding them. No worries if any surrounding chars get caught up: we can use capture groups to filter out the numbers. I'm unable to come up with an ECMAscript regular expression. Here is the playground: abcd %1 %%2 %%%3 %%%%4 efgh abcd%12%%34%%%666%%%%11efgh A successful catch will behave like this: Things I have tried: If you have realised, the third attempt is almost working. The only problems are in the second line of playground.

Node exits before async function completes

こ雲淡風輕ζ 提交于 2019-12-05 01:34:21
I have a function that returns a promise, and I am trying to await on it from within an async function. The problem is that the program completes immediately, instead of awaiting the promise. async-test.js: function doItSlow() { const deferred = new Promise(); setTimeout( () => { console.log( "resolving" ); deferred.resolve(); }, 1000 ); return deferred; } async function waitForIt( done ) { console.log( "awaiting" ); await doItSlow(); console.log( "awaited" ); done(); } waitForIt(() => { console.log( "completed test" ); }); console.log( "passed by the test" ); Build and run: babel --stage 0 -

TypeScript Type-safe Omit Function

旧巷老猫 提交于 2019-12-04 13:05:01
问题 I want to replicate lodash's _.omit function in plain typescript. omit should return an object with certain properties removed specified via parameters after the object parameter which comes first. Here is my best attempt: function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): {[k in Exclude<keyof T, K>]: T[k]} { let ret: any = {}; let key: keyof T; for (key in obj) { if (!(keys.includes(key))) { ret[key] = obj[key]; } } return ret; } Which gives me this error: Argument of

Using a Decorator to get list of implemented interfaces

不羁的心 提交于 2019-12-04 09:23:50
问题 Do you know if it is possible to get the array of interfaces implemented by a class using a decorator: interface IWarrior { // ... } interface INinja { // ... } So If I do something like: @somedecorator class Ninja implements INinja, IWarrior { // ... } At run-time Ninja will have an annotation which contains ["INinja", "IWarrior"] ? Thanks 回答1: Currently, types are used only during development and compile time. The type information is not translated in any way to the compiled JavaScript code

Clean way to keep original variable and destruction at the same time

房东的猫 提交于 2019-12-04 03:22:51
Is there a cleaner way to do this (with anything that is at least an ES draft and has a babel plugin, i.e., ES6, ES7, etc.): const { a, b } = result = doSomething(); Where I want to keep the overall result as one singular object, but also destructure it at the same time. It technically works, but result is implicitly declared (with an implicit var ), while I'd really like it to also be a const. I'm currently doing this: const result = doSomething(); const { a, b } = result; Which again works, but it's slightly on the verbose side, since I need to repeat this pattern dozens of times. I'd

Is there support for static typing in ECMAScript 6 or 7?

一世执手 提交于 2019-12-03 22:11:18
Is there any support for static typing in ECMAScript 6? How about ECMAScript 7? No. But on the ECMA-Script Wikipage there is a paragraph about changes in ECMA-Script 7: The Seventh Edition is in a very early stage of development, but is intended to continue the themes of language reform, code isolation, control of effects and library/tool enabling from ES6. New features proposed include promises/concurrency, number and math enhancements, guards and trademarks (an alternative to static typing) , operator overloading, value types (first-class number-like objects), new record structures (records,