Deeper Coding Concepts Flashcards
What’s the difference between a static function and an instance function?
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.
Would you ever initialize state in a React component with props?
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.
What’s the difference between inheritance and composition?
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.
What are the potential downsides of using inheritance?
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.
Can static functions use instance variables or call instance methods directly?
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 do lifecycle methods in React relate to state initialization with props?
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.
What is one common use case for static functions?
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.
In React, why might you use composition over inheritance?
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 do you update the state of a React component when the props change?
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.
What is an example of composition in React?
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>.
Can you give an example of a static function in JavaScript?
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.
Can you provide an example of an instance function in JavaScript?
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.