Why use an ES6 Map instead of a plain javascript object?

Anything can be used as a key in a map. Maps are ordered, and that allows for iteration. Combining 1 and 2, when you iterate over a map, you’ll get a useful array of key-value pairs! Check out the map.prototype.forEach() documentation. Source: Another good question/answer exchange. Worth marking this one as a duplicate. Update: Adding … Read more

ES6 module scope

Can I use “a” variable in a global scope after module importing or is it available only in a module scope? It’s only available inside the module it was declared in. Will ES6 modules have a similar working principle like this trick: […] Basically yes. ES6 has these kinds of scopes, order from “top” to … Read more

Difference between using a spread syntax (…) and push.apply, when dealing with arrays

Both Function.prototype.apply and the spread syntax may cause a stack overflow when applied to large arrays: let xs = new Array(500000), ys = [], zs; xs.fill(“foo”); try { ys.push.apply(ys, xs); } catch (e) { console.log(“apply:”, e.message) } try { ys.push(…xs); } catch (e) { console.log(“spread:”, e.message) } zs = ys.concat(xs); console.log(“concat:”, zs.length) Use Array.prototype.concat instead. … Read more

Mocha + TypeScript: Cannot use import statement outside a module

Had the same issue and almost gave up using Mocha with TypeScript (in our case Angular 9). This is what helped me: In tsconfig.json: Replaced this: “module”: “esnext”, with this: “module”: “commonjs”, Also here I found a working example of Mocha with TypeScript and used the tsconfig file from there to compare with mine: https://medium.com/@RupaniChirag/writing-unit-tests-in-typescript-d4719b8a0a40

JS Generators: How is `return yield` different from `yield`?

First, I’ll start by saying that Generators are a somewhat complicated topic, so giving a complete overview here won’t be possible. For more information I’d highly recommend Kyle Simpson’s You Don’t Know JS series. Book 5 (Async & Performance) has an excellent discussion on the ins and outs of generators. Onto the specific example you … Read more

Vue.js: How to specify props in single file component?

After a long time of experiment, I found out a practical solution: The project file structure: src/ assets/ components/ Home.vue App.vue main.js package.json config.js index.html Now, we want to access the root component — App‘s vm fields inside the sub-component Home.vue, with vue-route on. main.js: import Vue from ‘vue’ import VueRouter from ‘vue-router’ import App … Read more

Is it possible write a gulpfile in es6?

Yes, you can by using babel. Make sure you’ve got the latest version of the gulp-cli. npm install -g gulp-cli Install babel as a dependency of the project. npm install –save-dev babel Rename gulpfile.js to gulpfile.babel.js Your gulpfile might look something like this: import gulp from ‘gulp’; gulp.task(‘default’, () => { // do something }); … Read more