JS has come a long way since 1995, when it was named after Java in order to capitalize on the Java hype train.
ECMAScript Proposals goes through stage 0 to 4, 0 for its inception and 4 being ‘finished’.
As there are transpilers (Babel) available, we can make full use of new JS api and syntax with lesser worry about converting it to ES5 for older browsers.
‘this’ scenarios
- object.function() // object
- function but NOT bound to object // undefined(use strict), or global (in node), or window (browser)
- class constructor // instantiated object
- arrow function // lexical scope
- bind() //create function force it to bind to object (w args)
call() //call a function with bound object (w args)
apply() // same as call, but with [] outside of args (for array of args)
Rest or Spread operators/ Destructuring
spread operator – ... spreads the elements in an iterable (for joining/ insertion of arrays, etc)
rest operator – ... collects the elements in an iterable (as parameters for arguments, etc)
Sidenote: Modules
Modular systems: ES6 (browser/ node) or CommonJS (node)
CommonJS
ES6
Async in JS
Concepts to understand:
- Process
- Thread
- Event Loop
- Sync/ Async
Analogy: Multi-counter queue with idle clerks (multi-thread) vs one counter for only people who are ready (async)
Usage: To Avoid Callback Hell
Sidenote: Why Async?
Computational tasks vs I/O tasks
From wiki:
asynchronous I/O (also non-sequential I/O) is a form of input/output processing that permits other processing to continue before the transmission has finished.
Input and output (I/O) operations on a computer can be extremely slow compared to the processing of data. An I/O device can incorporate mechanical devices that must physically move, such as a hard drive seeking a track to read or write; this is often orders of magnitude slower than the switching of electric current. For example, during a disk operation that takes ten milliseconds to perform, a processor that is clocked at one gigahertz could have performed ten million instruction-processing cycles.
Since JS is a single thread process, async I/O becomes essential so that users do not get a ‘laggy’ user experience.
