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

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