Challenge - 5 Problems
Readonly Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when you try to modify a readonly reactive object?
Consider the following Vue 3 code using
readonly. What will be the console output after clicking the button?Vue
import { ref, readonly } from 'vue'; const state = ref({ count: 0 }); const readonlyState = readonly(state.value); function increment() { try { readonlyState.count++; } catch (e) { console.log('Error:', e.message); } } // Simulate button click increment(); console.log(readonlyState.count);
Attempts:
2 left
💡 Hint
Readonly objects prevent direct modification and throw errors in strict mode.
✗ Incorrect
The readonly wrapper in Vue 3 creates an immutable reactive proxy. Trying to change its properties throws an error in strict mode. The count remains unchanged at 0.
❓ state_output
intermediate2:00remaining
What is the value of the reactive state after wrapping with readonly?
Given this Vue 3 code, what will be the output of the last console.log statement?
Vue
import { reactive, readonly } from 'vue'; const state = reactive({ name: 'Alice' }); const readOnlyState = readonly(state); state.name = 'Bob'; console.log(readOnlyState.name);
Attempts:
2 left
💡 Hint
Readonly wraps the reactive object but reflects changes made to the original reactive source.
✗ Incorrect
The readonly proxy reflects the current state of the original reactive object. Changing state.name to 'Bob' updates readOnlyState.name as well.
📝 Syntax
advanced2:00remaining
Which code snippet correctly creates a deeply readonly reactive object?
Select the option that creates a deeply readonly reactive object in Vue 3.
Attempts:
2 left
💡 Hint
Readonly should wrap the reactive object to make it deeply readonly.
✗ Incorrect
Wrapping a reactive object with readonly creates a deeply readonly reactive proxy. Option D does this correctly.
🔧 Debug
advanced2:00remaining
Why does this Vue 3 code not prevent mutation of nested properties?
Examine the code below. Why can
state.user.name still be changed despite using readonly?Vue
import { readonly } from 'vue'; const state = { user: { name: 'John' } }; const readOnlyState = readonly(state); readOnlyState.user.name = 'Jane'; console.log(readOnlyState.user.name);
Attempts:
2 left
💡 Hint
Readonly is shallow by default unless wrapping a reactive object.
✗ Incorrect
The readonly function creates a shallow readonly proxy. Nested objects inside a plain object remain mutable unless they are reactive.
🧠 Conceptual
expert3:00remaining
How does Vue 3's readonly differ from Object.freeze for immutability?
Which statement best describes the difference between Vue 3's
readonly and JavaScript's Object.freeze when used for immutable data?Attempts:
2 left
💡 Hint
Think about reactivity and mutation prevention differences.
✗ Incorrect
Vue's readonly creates a reactive proxy that tracks dependencies and prevents mutations, enabling reactive updates. Object.freeze only prevents mutations but does not provide reactivity.