w03 Functions and Modules

Functions and Evolution

2022-01-27T15:30:00-05:00

  1. Old-school JS and modern JS
  2. History and evolution
  3. Functions three ways

Slides

https://comp426-2022-spring.github.io/slides/w03-00.html

Evolution

JavaScript History - w3schools

Difference between ES5 and ES6 - GeeksforGeeks

Difference between ES6 and TypeScript

Difference between JavaScript and TypeScript

Functions

Functions - MDN Web Docs

JavaScript Functions

Javascript Intro to Functions - Learn.co

JavaScript Functions - JavaScript Tutorial

Functions - learn-js.org

JavaScript Function Definitions

ES6 arrow functions

Javascript Arrow Function

Arrow functions for beginners - Brandon Morelli

Arrow functions vs regular functions in JavaScript - Madhavan Nagarajan

When (and why) you should use ES6 arrow functions — and when you shouldn’t - Cynthia Lee

TypeScript functions

The TypeScript Handbook

Introduction to TypeScript - GeeksforGeeks

More on Functions - TypeScript

Notes

History

JavaScript was invented in 1995 by Brendan Eich.

In 1996, it was developed into a standard scripting langage by ECMA’s (then the European Computer Manufacturers Association) Technical Committe (TC39) and released as ECMAScript 1 (ES1) in 1997.

TypeScript was developed and released by MicroSoft in 2012 to deal with some of the deficits of JS as a programming language.

It is technically a superset of ES6 (which is a superset of ES5).

The current release of TypeScript (TS4.5) incorporates ES2022 modules and is supported by Node.js 16.x.x.

Semicolons

It turns out that the seemingly arbitrary and undisciplined way I use semicolons is… fine I guess?

I have always just used them intuitively based on what I thought was happening and I realize now that my usage was a vestige of writing bash scripts, where they have somewhat more explicit use as a command terminator. So when I would write one-liners, this was the mechanism that I used to put the lines together into one.

Apparently some people get mad about semicolons in JS.

This is a good discussion of what semicolons are doing in JS and where the parser inserts them automatically and why: https://www.freecodecamp.org/news/lets-talk-about-semicolons-in-javascript-f1fe08ab4e53/

So, my advice is to use them if you feel like it and if you ever encounter someone who gets super upset about them, then avoid that person.

This

Regular functions bind their own scope whereas arrow functions inherit from the parent scope.

What does this mean?

It means that arrow functions are limited in terms of what they can and can’t do:

  • No binding to this
  • Can’t use as methods or constructors
  • No good for bind(), call(), and apply()
  • No good for anything that rely on a scope

But they can be used with closures and are nice for when we are creating API endpoints, which we will do next week.

Modules and export

2022-02-01T15:30:00-05:00

Slides

https://comp426-2022-spring.github.io/slides/w03-01.html

Modules, export, and import

Export and Import - JavaScript.info

The module object - Node.js

Node.js export module - GeeksforGeeks

Node Module Exports Explained – With JavaScript Export Function Examples - Stanley Nguyen

JavaScript Modules

Export

Import

ESM and CommonJS issues

You’re going to run into issues related to using the module, export, import.

Basically, you can’t use require('blahblah') with ESM modules and you can't use import` with CJS modules.

BUT THAT IS OKAY!

Because there is a way to make it happen.

Module API module.createRequire() - Node.js

Parsing command line arguments

How to parse command line arguments - Node.js

Command Line Arguments in Node.js - Scott Robinson

How to Parse Command Line Arguments in Node.js - Rahul Kumar