Computed Properties and Watchers Flashcards
1
Q
Describe the usefulness of computed properties
A
- In-template expressions are convenient, but are meant for simple operations that should only very few times in the template
- Too much logic in the templates make them hard to maintain and read
- A template should be simple and declarative, not full of logic
- For complex logic, or logic that appears multiple times, computed properties should be used
2
Q
What is defining a computed property really doing?
A
- It’s using the function we define as the getter function for an automatically-created property of the same name
3
Q
Describe the difference between computed properties and methods.
A
- Computed properties are cached based on their dependencies
- Methods always run when a re-render happens
- Computed properties automatically update their bindings when a dependency changes
- Methods do not, because they’re not properties
4
Q
Compare the styles of watch properties and computed properties
A
- Watch properties are imperative, and can be overly verbose
- Computed properties are terse, delcarative, and inheritantly watch all of the depdencies
5
Q
Describe computed setters.
A
- Computed properties getters-only by default.
- You can include a setter (
set() {...
), if you need to also set the computed property (in essence setting it’s dependencies which will set the computed property)
6
Q
When is the best time to use a watcher?
A
When you want to perform asynchronous or expensive operations in response to changing data.