Skip to content

QuizNot attempted

Proxy vs defineProperty

Why could Vue 3 detect obj.newKey = 1 when Vue 2 could not?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Vue 3 wraps objects in a Proxy, whose traps see operations on ANY key; Vue 2 used Object.defineProperty per existing key
  • A Proxy's traps (get, set, has, deleteProperty) see operations on ANY key of the wrapped object, including keys added later
  • Vue 2 used Object.defineProperty getters/setters per key that existed at setup, so new keys needed Vue.set.
  • Not polling: Vue never polls.
  • Not getters on Object.prototype: that would affect every object and still miss own new keys.
  • Not freezing: frozen objects can't be reactive at all.