JavaScript

JavaScript is a programming language.

It has many awkward edge cases and clunky idioms.

My JavaScript Snippets.

ES6 (ES2015)

ECMAScript 2015 introduced a class model.

Async Iterators

Async iterators are a candidate feature (stage 3 Proposal) for ECMAScript that allows iteratators to produce results asynchronously. Async interators can be only used inside asynchronous functions.

for await (let line of readlines()) {
  console.log(line);
}

To build an asynchronous source of data, async generators can be used:

async function * readlines() {
  for (let i = 0; i < 10; i++) {
    yield getRandomLine();
  }
}

Engines

ChakraCore

ChakraCore is an open source JavaScript engine that powers Microsoft Edge and Windows 10 applications. It supports JIT compilation, garbage collection, and a wide range of ESNext features.

Generators

Generators are a particular case of coroutines. A coroutine is a procedure (function, method) that retains its execution state between consecutive calls.

Console

The console module provides functions to print information on both stdoutand stderr streams.

console.log prints to stdout with the newline at the end. It supportsprintf-like formating (%s string, %i integer, %o object, %f float,%d decimal)

console.log('You age is %d', 17);

Also, console.log output can be formatted using CSS:

console.log("%cThe text is blue and the background is pink", "color: blue; background-color: pink;")

console.error is exactly as console.log, but it prints to stderr instead of stdout.

console.dir formats its arguments using util.inspect and prints the result to stdout.

console.time(label) starts a timer with the label as a bookmark, whileconsole.timerEnd(label) ends a timer started with label, and it prints the elapsed time to stdout.

console.trace~ prints out a stack trace where it's invoced.

console.count(label) prints how many times a log with the labal has been printed

console.assert(condition) prints only if the condition is not satisfied

console.assert(n % 3 === 0, "n is not divisable by 3")

console.table prints objects in tabular way using three columns: index, key, value.

console.group allows to group other logs with a graphical delimiters.

Modules

ECMAScript Modules

The ECMAScript modules standard uses the export and import keywords.

CommonJS

The CommonJS standard uses module.exports and require() function.

Copyright © 2019 All rights reserved.
Powered by Kulfon.