Deeper Coding Concepts Flashcards

1
Q

What’s the difference between a static function and an instance function?

A

A static function is associated with the class itself, not with an instance of the class. It can be called without creating an object of the class. An instance function is associated with an instance of the class (an object) and can typically access data that pertains only to that instance.

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

Would you ever initialize state in a React component with props?

A

Yes, but with caution. It’s acceptable to initialize state in a React component with props if you do not expect the props to change, or if the initial state is a one-time setup that doesn’t rely on props updates. If props change over time, you should use the componentDidUpdate lifecycle method or the useEffect hook to update the state accordingly, to keep it in sync with the props.

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

What’s the difference between inheritance and composition?

A

Inheritance is a concept where a class (child or subclass) inherits properties and behavior (methods) from another class (parent or superclass). Composition, on the other hand, involves building classes and objects by combining different, more straightforward objects, allowing for more flexibility as it’s a has-a relationship instead of an is-a relationship that you see with inheritance.

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

What are the potential downsides of using inheritance?

A

Inheritance can lead to tightly coupled code, making the system harder to understand and maintain. It can also introduce complexity through the inheritance hierarchy and can cause unexpected side effects when changes are made in the parent class.

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

Can static functions use instance variables or call instance methods directly?

A

No, static functions cannot access instance variables or call instance methods directly because they belong to the class as a whole, not to any specific object instance.

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

How do lifecycle methods in React relate to state initialization with props?

A

Lifecycle methods in React, such as componentDidMount, componentDidUpdate, and getDerivedStateFromProps, allow you to handle changes in props over time and update the state accordingly, ensuring that the component reflects the new props.

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

What is one common use case for static functions?

A

One common use case for static functions is utility or helper functions that perform a task that does not depend on the state of any particular instance of the class.

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

In React, why might you use composition over inheritance?

A

React recommends using composition over inheritance to reuse code between components. Composition gives you a more flexible approach to design your components and customize them without being tied to a specific component hierarchy.

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

How do you update the state of a React component when the props change?

A

To update the state when props change, you can use the getDerivedStateFromProps lifecycle method in class components or the useEffect hook in functional components, which listens for changes in props and updates the state accordingly.

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

What is an example of composition in React?

A

An example of composition in React is when a component passes its children down to a sub-component without having to know about the internal details of the sub-component, like <ParentComponent><ChildComponent></ChildComponent></ParentComponent>.

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

Can you give an example of a static function in JavaScript?

A

Yes. Imagine a utility class that converts temperatures from Celsius to Fahrenheit. The conversion method does not depend on instance variables, so it makes sense as a static method.

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

Can you provide an example of an instance function in JavaScript?

A

Sure. Consider a class that represents a simple bank account. An instance function might be used to handle deposits to an individual account, which would need to access or modify the instance’s balance property.

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