Unit_10: JavaScript basics Flashcards

1
Q

n JavaScript, what happens if you pass fewer or more parameters than expected to a function?

A

because Parameters are more of a suggestion

-> Too few? The rest will have a value of undefined
-> Too many? They don’t have names, but they are
there (inside the arguments array which contains all the parameters passed to the function, even those not explicitly named in the function definition.)

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

in JavaScript, how do you declare a private instance variable in a class?

A

adding the # symbol in front of the variable name (e.g., #id).

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

What does the # symbol indicate when used in front of an instance variable in JavaScript?

A

instance variable is private and cannot be accessed directly outside of the class.

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

How can you assign a value to a private instance variable in JavaScript?

A

You assign a value to a private instance variable using the this.#variableName syntax inside the class constructor or methods.

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

In the following JavaScript class, what is the access level of #id?
class MyClass {
#id;
constructor(id) {
this.#id = id;
}
}

A

The #id variable is private and can only be accessed within the MyClass class.

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

How does JavaScript handle function/method overloading?

A

JavaScript does not support classical function/method overloading. Only the last version of the function or method is used, and it overwrites any previous definitions.

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

If you try to overload a function or method in JavaScript, what happens?

A

The last version of the function/method is used, and the earlier versions are ignored. Only the most recent definition is considered.

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

How can you simulate function overloading in JavaScript?

A

You can simulate overloading by using the arguments array to check the number and types of arguments passed to the function, allowing you to handle different cases manually.

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

What are accessors and mutators in JavaScript?

A

Accessors (getters) retrieve the value of a property, while mutators (setters) allow you to set or modify the value of a property.

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

How can you modify the value of #id in the MyClass object?

A

You can modify the value of #id by using the setter method, like mc.id = 7500;, which updates the value of #id internally.

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

What is a static method in JavaScript?

A

A static method is a method that belongs to the class itself, rather than instances of the class. It can be called directly on the class, like MyClass.HOURS_PER_DAY.

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

What is the purpose of using the get keyword for defining the static method in the MyClass example?

A

The get keyword makes the static method behave like a class constant, allowing access to the value as if it were a property, e.g., MyClass.HOURS_PER_DAY instead of a function call.

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

How do you access the static field HOURS_PER_DAY in the MyClass example?
class MyClass {

static get HOURS_PER_DAY(){
return 24;
}
}

A

directly on the class using console.log(MyClass.HOURS_PER_DAY);.

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