W3 Flashcards

1
Q

create an object named “obj” with data members “property_1: value_1” and “property_2:value_2” using object literal notation

A
var obj = { property_1:   value_1,   
            property_2:   value_2
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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 };
A

setAge: function(newAge){this.age = newAge}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Use the built in “Object.create()” function to create a new instance of an “architect” object named “architect1”

A

var architect1 = Object.create(architect);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

call the “setName” function on an architect object named “arch2”. Set the name to “Mary”

A

arch2.setName(“Mary”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

create an “architect” object using a function constructor with the properties name, age, and occupation. inName and inAge are passed as arguments

A
function architect(inName, inAge){
    this.name = inName;
    this.age = inAge;
    this.occupation = "architect";
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Use the architect function prototype to create a new method “setName” that takes the parameter “newName”

A

architect.prototype.setName = function(newName){this.name = newName};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Differences between initializing with var, let, and const

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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;}

}

A

constructor(setName = “”, setAge = 0){ // handle missing parameters
this.name = setName;
this.age = setAge;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Properties within classes are public by default. How do we declare a property as private?

A

Use the prefix ‘#’ before the identifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to catch an exception on a block of code

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Output an error message within a catch block

try{
    PI = 99;
}catch(ex){
  // Code here
}
A

console.log(ex.message);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to execute code in a try catch block regardless of if the code runs successfully

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to create a custom exception with an error message

A

use the throw keyword

ex.
    if(y == 0){
    throw new Error("Division by Zero!");
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to guarantee the order of execution in a program (have functions run in the intended order)

A

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!”);
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to return data after a Promise

A

Pass the data to the resolve object, and output the data message

ex.
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("outputA resolved!"); // 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(data){
console.log(data);
});

17
Q

What keyword can we use in a promise if the function is not guaranteed to execute?

A

We can use “reject” instead of “resolve” and output the reason within a catch block

ex.

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("-");
            reject("outputA rejected!"); // call "reject" because the function encountered an error
        },randomTime);
    });   
}

// call the outputA function and when it is “resolved” or “rejected, output a confirmation to the console

outputA()
.then(function(data){
    console.log(data);
})
.catch(function(reason){
    console.log(reason);
});
18
Q

How can we chain together Promise objects to execute one after the other?

A

using the “then” keyword

ex.

outputA()
.then(outputB)
.then(outputC)

19
Q

syntax for arrow functions:

A

parameter => logic

20
Q

When are arrow functions most useful when trying to simplify the syntax?

A

When the function takes no parameters.

ex.

var outputMessage = function() {
    console.log("Hello Function Expression");
};

// is the same as:

var outputMessageArrow = () => console.log(“Hello Arrow Function”);

21
Q

Explain these features of arrow functions:

Implicit return statement
Lexical “this”

A

Implicit return statement:
returns the value of the statement, without having to use the “return” keyword

ex.
var adderArrow = (num1, num2) => num1 + num2;

Lexical “this”: Uses the “this” value of the parent scope.

ex.
architect.prototype.outputNameDelay = function(){
setTimeout(() => { console.log(this.name); }, 1000);
};