cannot export const arrow function

You’re trying to export a default and declare a variable at the same time, which is invalid syntax. Consider the following, since we know that you can declare multiple variables using only one instance of the keyword, var a, b, c; the export definition wouldn’t make sense at all. export default var a, b, c; … Read more

Multiline strings that don’t break indentation

2020 answer: there is still nothing built into the JS stdlib to handle de-denting long lines, although TC39 has discussed adding a new template literal that handles indentation. You have 2 options presently: The endent and dedent-js packages will handle this. Note the dedent-js package actually works with both tabs and spaces. var dedent = … Read more

ES6 Modules: Undefined onclick function after import

Module creates a scope to avoid name collisions. Either expose your function to window object import {hello} from ‘./test.js’ window.hello = hello Or use addEventListener to bind handler. Demo <button type=”button” id=”hello”>Click Me</button> <script type=”module”> import {hello} from ‘./test.js’ document.querySelector(‘#hello’).addEventListener(‘click’, hello) </script>

ECMAScript 2015: const in for loops

The following for-of loop works: for (const e of a) The ES6 specification describes this as: ForDeclaration : LetOrConst ForBinding http://www.ecma-international.org/ecma-262/6.0/index.html#sec-for-in-and-for-of-statements-static-semantics-boundnames The imperative for loop will not work: for (const i = 0; i < a.length; i += 1) This is because the declaration is only evaluated once before the loop body is executed. http://www.ecma-international.org/ecma-262/6.0/index.html#sec-for-statement-runtime-semantics-labelledevaluation

When is the body of a Promise constructor callback executed?

Immediately, yes, by specification. From the MDN: The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object) This is defined in the ECMAScript specification (of course, it’s harder to read…) here (StepĀ 9 as of this edit, showing … Read more

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

You can use object spread to have an optional property: let flag1 = true; let flag2 = false; // extra cases added by Abdull let optionalKey8 = 8; let optionalKey9 = undefined; let optionalKey10 = false; let optionalKey11 = null; let optionalKey12 = “twelve”; const obj = { requiredKey1: 1, requiredKey2: 2, …(flag1 && { … Read more

Syntax for destructuring arrays into function parameters in ES6

The correct syntax to destructure an array parameter is: function foo([a, b]) { console.log(`param1: ${a}, param2: ${b}`); } It can be called like this: foo([‘first’, ‘second’]); // Will output: // param1: first, param2: second According to Exploring ES6, section 11.6, you can use this to destructure parameters within arrow functions as well: const items = … Read more

How to listen for value changes from class property TypeScript – Angular

You can still check component’s field members value change by KeyValueDiffers via DoCheck lifehook. import { DoCheck, KeyValueDiffers, KeyValueDiffer } from ‘@angular/core’; differ: KeyValueDiffer<string, any>; constructor(private differs: KeyValueDiffers) { this.differ = this.differs.find({}).create(); } ngDoCheck() { const change = this.differ.diff(this); if (change) { change.forEachChangedItem(item => { console.log(‘item changed’, item); }); } } see demo.