Created by
Hazel Campbell (hazel.campbell@ualberta.ca).
Copyright 2023.
<script> tags
'use strict';
module.exports.safeAdd = function(a, b) {
const s = a + b;
if (Number.isSafeInteger(s) && Number.isSafeInteger(a) && Number.isSafeInteger(b)) {
return s;
} else {
throw Error("Numbers got too big!");
}
}
module.exports.safeSubtract = function(a, b) {
const s = a - b;
if (Number.isSafeInteger(s) && Number.isSafeInteger(a) && Number.isSafeInteger(b)) {
return s;
} else {
throw Error("Numbers got too big!");
}
}
safety.js
'use strict';
const safeAdd = function(a, b) {
// some code ...
}
const safeSubtract = function(a, b) {
// some code ...
}
module.exports = { safeAdd, safeSubtract };
something else in the same folder...
'use strict';
const {safeAdd, safeSubtract} = require('./safety'); // note no .js
console.log(safeAdd(2, 2));
safety.mjs
// don't need use strict, its on by default in ES modules!
export function safeAdd(a, b) {
// some code ...
}
export function safeSubtract(a, b) {
// some code ...
}
something else in the same folder...
import {safeAdd, safeSubtract} from './safety.mjs'; // we do need the .mjs here
console.log(safeAdd(2, 2));
Async loading...
async function loadAndSubtract() {
safety = await import("./safety.mjs");
console.log(safety.safeAdd(2, 2));
}
.mjs
to tell Node and other libraries that it's an ES Module..esm
or .esm.js
are ES modules, but not recognized by Node..cjs
to tell Node and other libraries that it's an CommonJS Module (rare).await
outside of an async function
gcc -g
, but for the web//# sourceMappingURL=yourFileName.min.js.map
...
const f = (...a) => {
// a is an array of all the arguments passed in
console.log(...a); // give console.log multiple arguments
const b = [0, ...a]; // b is a new array with 0, then the elements from a
console.log(...b);
}
f(1, 2, 3) // works just like console.log(1, 2, 3)
Copyright 2014-2023 ⓒ Abram Hindle
Copyright 2019-2023 ⓒ Hazel Victoria Campbell and contributors
Other images used under fair use and copyright their copyright holders.
Copyright (C) 2019-2023 Hazel Victoria Campbell
Copyright (C) 2014-2023 Abram Hindle and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN.
01234567890123456789012345678901234567890123456789012345678901234567890123456789