Web Fundamentals
Web Scripting

Ruben Taelman, Ghent Universityimec

Web Fundamentals
Web Scripting

Ruben Taelman

Ghent University imec IDLab

Creative Commons License Except where otherwise noted, the content of these slides is licensed under a Creative Commons Attribution 4.0 International License.

JavaScript, the programming language

[Logo of JavaScript.]

JavaScript has good and bad parts

In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders.

Douglas Crockford (2008). “JavaScript: The Good Parts: The Good Parts”, p.2, "O'Reilly Media, Inc."

Effectively using JavaScript

Web Fundamentals
Web Scripting

Web Fundamentals
Web Scripting

Web Fundamentals
Web Scripting

Objects represent things

Objects have properties and internal slots

Property descriptors encode attributes of properties

Data descriptors encode attributes of value-based properties

Object.defineProperty(dog, "name", { value: "Laika" });

Accessor descriptors encode attributes of getter-setter-based properties

Object.defineProperty(dog, "name", { get: () => "Laika" });

Common attributes for data and accessor descriptors

Property descriptors allow fine-grained control

Web Fundamentals
Web Scripting

Prototype-based inheritance

[Property access propagates through property chain.]

Creating prototypes manually

Defining prototype chain manually

Instantiating a prototype

Avoid defining prototypes manually!

Web Fundamentals
Web Scripting

Type Coercion implicitly converts values to the expected type

[Non-strict equality operator is not transitive.]
©2022 https://github.com/denysdovhan/wtfjs
[Plus operator also experiences strange behaviour.]
©2019 Mainasara Al-amin Tsowa

Internal ToPrimitive() function is used for many coercion algorithms

ToPrimitive() uses a fall-through approach

Example: Applying +

Example: Applying +

Example: Applying +

Example: Applying +

Avoid Type Coercion when possible

Web Fundamentals
Web Scripting

Event-based runtime model

Callback functions for (a)synchronous completion

Engines process the macrotask queue

[Example of the Macrotask queue.]
©2023 Ilya Kantor
[Stack, Heap, and Queue.]
©2023 MDN

Promises hold results of async operations

async/await: syntactic sugar for promises

Promises always handled asynchronously

Microtasks run before macrotasks

Task scheduling impacts performance

[Example of the Macrotask and Microtask queues.]
©2023 Ilya Kantor

Web Fundamentals
Web Scripting

Iterables and iterators

Generators provide syntactic sugar to create iterators

Async iterators are iterators returning promises

Async generators provide syntactic sugar to create async iterators

Web Fundamentals
Web Scripting

Metaprogramming: writing a program that can process itself

[Ouroboros]

Proxies can intercept and modify object operations

Web Fundamentals
Web Scripting

Building blocks for parallelism

Typed Arrays: Low-level binary arrays

Typed Arrays require specific memory allocation

Web Fundamentals
Web Scripting

Web Fundamentals
Web Scripting

[Logo of Node.js.]

Run JavaScript Everywhere

The Node.js principles

Node.js is based on the V8 engine

Reactor pattern for handling I/O

[Asynchronous I/O using the reactor pattern in Node.js.]
©2020 Node.js Design Patterns

Node.js implements the event loop as multiple sequential phases

Node.js is the most popular environment, but alternatives exist

Web Fundamentals
Web Scripting

Splitting up code into modules

Node.js supports two types of modules

CommonJS: Node.js's original modules

Module resolution in CommonJS

const imported = require(mod);

Examples of module resolution in CommonJS

require('url')
Node.js URL core module
require('./file')
./file.js
require('pad')
./node_modules/pad/index.js

ECMAScript modules: Native JavaScript modules

Differences between CJS and ESM

CJS and ESM in practise

[CJS is still more popular on npm, but declining.]
©2024 https://github.com/wooorm/

Web Fundamentals
Web Scripting

A package managers helps managing collections of JavaScript modules

[Logo of npm.]

A package manager for Node.js

Node.js was created before npm

npm is the most popular package managers, but alternatives exist

npm packages follow Semantic Versioning (SemVer)

SemVer is useful for man and machine

Dependencies can be defined using SemVer ranges

Installing dependencies with a package manager

A naive dependency resolution algorithm: nested node_modules/

[Node modules are the heaviest objects in the universe.]

Dependency resolution with deduping

Dependencies that are incompatible with the SemVer range are not hoisted

The order in which dependencies are installed determines matters

Web Fundamentals
Web Scripting

From callbacks to EventEmitters

[EventEmitter enables observation on multiple events.]
©2020 Node.js Design Patterns

Conventions for asynchronous error handling in Node.js

Errors can not be handled in callbacks and EventEmitters using try-catch.

Streams are EventEmitters for processing asynchronous data streams

Reading files using streams

Node.js Streams preceded the Web Streams API (for browsers)

Concepts in Node.js and Web Streams

Web Fundamentals
Web Scripting

JS engines use just-in-time (JIT) compilation

V8 has multiple types of compilers

Optimizing compilers learn shape and structure of running code

Optimizers are speculative

Web Fundamentals
Web Scripting

Development in JavaScript differs from other languages

Most JavaScript projects incorporate multiple development processes

Web Fundamentals
Web Scripting

Linters analyze code to find and fix potential problems

[Logo of ESLint.]

Examples of rules for ESLint

Categories of linter rules

[Linters report on problems per file.]

Web Fundamentals
Web Scripting

Increasing confidence in code through automated testing

Measuring tested domain as code coverage

[Logo of Jest.]

Jest is a popular testing framework for JavaScript

[Coverage reports show details of missed lines and branches.]

Web Fundamentals
Web Scripting

Transpilers make code compatible with different JavaScript versions

[Logo of Babel.]

Babel makes modern JavaScript backwards-compatible

[Logo of TypeScript.]

TypeScript is a superset of JavaScript with type definitions

tsc is the TypeScript compiler

Web Fundamentals
Web Scripting

Towards cross-platform JavaScript

Bundlers enable cross-platform JavaScript

[Webpack is a bundler that bundles JavaScript and other assets.]

Bundlers work in two steps

Making smaller bundles for production

[Minified code is difficult to read without source maps.]

Web Fundamentals
Web Scripting