Adv Conc- Modules, Exceptions, Garbage Collection, Side effects/Pure Functions Flashcards
What are the benefits of using modules? (3 things)
If a program is contained entirely in one file, even just 100 lines of code can take time to understand fully. Splitting the program up is easier to understand each component.
Working on a program as a team is easier with multiple files.
You have to put in more work to encapsulate and make private data properly.
Import a module managed by npm
const readline = require(‘readline-sync’);
let choice = readline.question(“Do you want to run this program? (y/n)”);
CommonJS modules are compatile with browsers
t or f
Browsers don’t natively support CommonJS modules, but you can use a transpiler like Babel to transpile code that uses CommonJS modules into a format that can be used by browsers.
Export and import a module
module.exports = anObject
let pear = require(“./CodeTester1”);
// it needs the path
hat happens if you have multiple exports?
let apple = {
a: 1,
}
let orange = {
a: 2,
}
module.exports = apple;
module.exports = orange;
let pear = require(“./CodeTester1”);
console.log(pear.a);
The last module.export to get processed is processed
export a module (for use in a compatible browser) (2 ways)
export let variable = primitive or object
or
export {variable}
import a module (for use in a compatible browser)
import { variableNameFromOtherFile } from “./filePathAndName.ext”
import multiple modules in one line (for use in a compatible browser)
import { variableName1, variableName2 } from “./filePathAndName.ext”
Import a module and change the variable name (for use in a compatible browser)
import { variableName1 as newVar1, variableName2 } from “./filePathAndName.ext”
Import all modules (for use in a compatible browser) and access them
File1.js
export function foo() {
console.log(1);
}
File2.js
import * as FooModule from “./File1”;
FooModule.foo(); // 1
What is this called?
File1.js
export function foo() {
console.log(1);
}
File2.js
import * as FooModule from “./File1”;
FooModule.foo(); // 1
Namespace import.
While using Namespace imports is possible, it is more common and preferred to use Named imports for importing specific declarations.
How many default exports are allowed per file
1
Do default export and import (for use in a compatible browser
bar.js
function bar() {
console.log(2);
}
export default bar;
otherfile.js
import bar from “./bar”;
bar(); // 2
Do default export and import and changed the name (for use in a compatible browser
bar.js
function bar() {
console.log(2);
}
export default bar;
otherfile.js
import renamedBar from “./bar”;
renamedBar(); // 2
Do default export and import and changed the name
Also import a non default module
(for use in a compatible browser)
utils.js
export function foo() {
console.log(1);
}
function bar() {
console.log(2);
}
export default bar;
otherfile.js
import renamedBar, { foo } from “./utils.js”;
foo(); // 1
renamedBar(); // 2
import and export are usually only compatible in browser.
Do it in node and make it compatible (2 ways)
change the file names to .mjs
Alternatively, a more preferred method is to add “type”: “module” to the package.json file. This enables ES6 modules and the files can retain the .js extension.
//package.json
{
“name”: “index”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“type”: “module”,
“scripts”: {
“test”: “echo "Error: no test specified" && exit 1”
},
“keywords”: [],
“author”: “”,
“license”: “ISC”
}
Create a custom error
throw new Error(“Divide by zero!”);
Catch and error and do a block
try {
} catch(errorObject) {
}
With a try catch statement, have it only do something if the error is of a certain kind
function divideOneBy(divisor) {
try {
let result = div(1, divisor);
console.log(result);
} catch (error) {
if (error instanceof DivideByZeroError) {
console.log(${error.message} ignored
);
}
}
}
divideOneBy(1); // 1
divideOneBy(0); // Divide by zero! ignored
With a try catch statement, have it only do something if the error is of a certain kind
If it’s another kind, let that error throw
function divideOneBy(divisor) {
try {
let result = div(1, divisor);
console.log(result);
} catch (error) {
if (error instanceof DivideByZeroError) {
console.log(${error.message} ignored
);
} else {
throw error;
}
}
}
divideOneBy(1); // 1
divideOneBy(0); // Divide by zero! ignored
What Happens When A Program Throws An Exception (3 things)
- js goes back to look for a try block to see if it contains the code. Looks back at all the blocks, if none are try blocks, throws the error
- if it finds a try block, it exits the try block (without executing anymore code) and executes the catch block
- If the catch block re-throws the exception, it again resumes as before, looking for another try block.
What happens with this:
try {
console.log(1);
a = b;
console.log(2);
} catch(error) {
console.log(3);
}
1
3
YOu should use exceptions for flow control
“if this throws an error, then do this other code”
t or f
F
What’s the idea of “ask forgiveness” in error throwing
– Computer programming pioneer Admiral Grace Hopper
Exceptions are sometimes likened to the “ask forgiveness” approach to error handling. Rather than asking whether we can do something, we just go ahead and try it. If an exception does occur, then we have to ask forgiveness – handle the exception or just accept the cost of an error.
In contrast to the “ask forgiveness” approach, the “get permission” approach involves determining beforehand whether something is going to work. For instance, if we expect a user to input a number, we can test whether they really did enter a number before we try to use it as a number. This is often harder to do than allowing an exception to be raised, but once permission is granted, we know things will work.