JaxNode October 2020
// addTwo.mjs
function addTwo(num) {
return num + 2;
}
export { addTwo };
// app.mjs
import { addTwo } from './addTwo.mjs';
// Prints: 6
console.log(addTwo(4));
function Person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.personAge = age;
return this;
}
Person.prototype.getFullname = function() {
return `${this.firstName} ${this.lastName}`;
}
Person.prototype.description = function() {
console.log(`${this.firstName} ${this.lastName} is ${this.personAge} years old.`)
}
class Person {
constructor(first, last, age) {
this.firstName = first;
this.lastName = last;
this.personAge = age;
}
getFullname() {
return `${this.firstName} ${this.lastName}`;
}
description() {
console.log(`${this.firstName} ${this.lastName} is ${this.personAge} years old.`)
}
}
function createPerson(first, last, age) {
function getFullname() {
return `${first} ${last}`;
}
function description() {
console.log(`${first} ${last} is ${age} years old.`);
}
return Object.freeze({
first,
last,
age,
getFullname,
description
});
}
let myArray = ['Dave', 'Piper', 'Harry', 'Scott'];
for (let i in myArray) {
console.log(myArray[i]);
}
for (let item of myArray) {
console.log(item);
}
let myMap = new Map([[ 'John', 34], ['Bill', 22], ['Burt', 79]);
myMap.set('Sarah', 29);
// You can also verify if there is a key value pair in your map
// by calling the has(key) function.
myMap.has('Sarah');
// You can return the value for a given key by using the get(key) function.
myMap.get('Sarah'); // returns 29;
"use strict";
function *getSomeValues() {
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
}
var someValues = getSomeValues();
console.log(someValues.next()); // yields { value: 1, done: false }
console.log(someValues.next()); // yields { value: 2, done: false }
console.log(someValues.next()); // yields { value: 3, done: false }
console.log(someValues.next()); // yields { value: 4, done: false }
console.log(someValues.next()); // yields { value: 5, done: false }
console.log(someValues.next()); // yields { value: undefined, done: true }
function listAnimalsES2015(...animals) {
animals.forEach(item => {
console.log(item);
});
}
listAnimalsES2015('dog', 'cat', 'platipus');
let customer = {
name: “Carl”,
details: { age: 82 }
};
const customerCity = customer?.city ?? “Unknown city”;
console.log(customerCity); // Unknown city
let customer2 = {
name: "David",
details: { age: 50 },
city: 'Jacksonville'
}
const customerCity2 = customer2?.city ?? “Unknown city”;
console.log(customerCity2); // Jacksonville
// browser environment
console.log(globalThis); // => Window {...}
// node.js environment
console.log(globalThis); // => Object [global] {...}
// web worker environment
console.log(globalThis); // => DedicatedWorkerGlobalScope {...}
// Example from https://www.ksharifbd.com/blog/exploring-ES2020-dynamic-import/
<script type="module">
import add from './add.js';
add(7, 4); // returns 11
</script>
<!-- We don't need to set the type -->
<script>
import('./add.js')
.then(module => module.default(7, 4)) //returns 11
.catch(error => // log error);
</script>
// this code resides in subtract.js file
const subtract = (a, b) => a - b;
export { subtract };
// in index.js file
import('./subtract.js')
.then(module => module.subtract(7, 4)) // returns 3
.catch(error => // log error);
const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo'));
const promises = [promise1, promise2];
Promise.allSettled(promises).
then((results) => results.forEach((result) => console.log(result.status)));
// expected output:
// "fulfilled"
// "rejected"
const theBiggestInt = 9007199254740991n
const alsoHuge = BigInt(9007199254740991)
// ↪ 9007199254740991n
const hugeString = BigInt("9007199254740991")
// ↪ 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff")
// ↪ 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111")
// ↪ 9007199254740991n