W3 Flashcards
create an object named “obj” with data members “property_1: value_1” and “property_2:value_2” using object literal notation
var obj = { property_1: value_1, property_2: value_2 };
Add a function named “setAge” that sets the age of the object using the “newAge” parameter
var obj = { property_1: value_1, property_2: value_2, // ..., "property n": value_n };
setAge: function(newAge){this.age = newAge}
Use the built in “Object.create()” function to create a new instance of an “architect” object named “architect1”
var architect1 = Object.create(architect);
call the “setName” function on an architect object named “arch2”. Set the name to “Mary”
arch2.setName(“Mary”);
create an “architect” object using a function constructor with the properties name, age, and occupation. inName and inAge are passed as arguments
function architect(inName, inAge){ this.name = inName; this.age = inAge; this.occupation = "architect"; }
Use the architect function prototype to create a new method “setName” that takes the parameter “newName”
architect.prototype.setName = function(newName){this.name = newName};
Why won’t this code output the architect’s name property?
architect.prototype.outputNameDelay = function(){ setTimeout(function(){ console.log(this.name); },1000); } // ... architect2.outputNameDelay(); // outputs undefined
Because the “this” keyword is within the function scope of the “setTimeout” function, which isn’t a method of the architect object
To resolve this, we can create a function variable named “that” and initialize it to “this”. We can also use function arrow syntax.
Differences between initializing with var, let, and const
var : Scope is its current execution content (function scope if declared within a function, or global outside any function)
let: block scope
const: block scope, unmodifiable value
create a constructor within this class that sets the name and age of the object
class architect{
name; age; occupation = "architect"; // default value of "architect" for occupation
// CONSTRUCTOR HERE
setName(newName){this.name = newName} setAge(newAge){this.age = newAge} getName(){return this.name;} getAge(){return this.age;}
}
constructor(setName = “”, setAge = 0){ // handle missing parameters
this.name = setName;
this.age = setAge;
}
Properties within classes are public by default. How do we declare a property as private?
Use the prefix ‘#’ before the identifier
How to catch an exception on a block of code
Use a try catch block. Example:
const PI = 3.14159;
console.log(“trying to change PI!”);
try{ PI = 99; }catch(ex){ console.log("uh oh, an error occurred!"); }
console.log(“Alas, it cannot be done, PI remains: “ + PI); // This code runs successfully
Output an error message within a catch block
try{ PI = 99; }catch(ex){ // Code here }
console.log(ex.message);
How to execute code in a try catch block regardless of if the code runs successfully
add a “finally” block
ex.
try{ PI = 99; }catch(ex){ console.log("uh oh, an error occurred: " + ex.message); // outputs: uh oh, an error occurred: Assignment to constant variable. }finally{ console.log("always execute code in this block"); }
How to create a custom exception with an error message
use the throw keyword
ex. if(y == 0){ throw new Error("Division by Zero!"); }
How to guarantee the order of execution in a program (have functions run in the intended order)
Use the “promise” keyword within a function, then call the function followed by the “then” keyword
ex.
// output "A" after a random time between 0 & 3 seconds function outputA(){ var randomTime = Math.floor(Math.random() * 3000) + 1;
return new Promise(function(resolve, reject){ // place our code inside a "Promise" function setTimeout(function(){ console.log("A"); resolve(); // call "resolve" because we have completed the function successfully },randomTime); }); }
// call the outputA function and when it is “resolved”, output a confirmation to the console
outputA().then(function(){
console.log(“outputA resolved!”);
});