Home / Function/ set() — vue Function Reference

set() — vue Function Reference

Architecture documentation for the set() function in index.ts from the vue codebase.

Function typescript VueCore Observer calls 3 called by 1

Entity Profile

Dependency Diagram

graph TD
  3f865d9f_787c_7a50_d9a7_bb5acf03eb88["set()"]
  af395f8e_1ac5_a239_71b7_fd29a1c03d2c["index.ts"]
  3f865d9f_787c_7a50_d9a7_bb5acf03eb88 -->|defined in| af395f8e_1ac5_a239_71b7_fd29a1c03d2c
  a46cd6c3_9d19_f5e0_7752_7009a1d4a572["mergeData()"]
  a46cd6c3_9d19_f5e0_7752_7009a1d4a572 -->|calls| 3f865d9f_787c_7a50_d9a7_bb5acf03eb88
  b757abbc_61ef_2454_445e_6bb830e92333["observe()"]
  3f865d9f_787c_7a50_d9a7_bb5acf03eb88 -->|calls| b757abbc_61ef_2454_445e_6bb830e92333
  dea83477_00f1_3c8b_f0a1_584882807d1f["defineReactive()"]
  3f865d9f_787c_7a50_d9a7_bb5acf03eb88 -->|calls| dea83477_00f1_3c8b_f0a1_584882807d1f
  98344c54_4454_3804_84b6_6f45b28782b9["notify()"]
  3f865d9f_787c_7a50_d9a7_bb5acf03eb88 -->|calls| 98344c54_4454_3804_84b6_6f45b28782b9
  style 3f865d9f_787c_7a50_d9a7_bb5acf03eb88 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/core/observer/index.ts lines 223–276

export function set(
  target: any[] | Record<string, any>,
  key: any,
  val: any
): any {
  if (__DEV__ && (isUndef(target) || isPrimitive(target))) {
    warn(
      `Cannot set reactive property on undefined, null, or primitive value: ${target}`
    )
  }
  if (isReadonly(target)) {
    __DEV__ && warn(`Set operation on key "${key}" failed: target is readonly.`)
    return
  }
  const ob = (target as any).__ob__
  if (isArray(target) && isValidArrayIndex(key)) {
    target.length = Math.max(target.length, key)
    target.splice(key, 1, val)
    // when mocking for SSR, array methods are not hijacked
    if (ob && !ob.shallow && ob.mock) {
      observe(val, false, true)
    }
    return val
  }
  if (key in target && !(key in Object.prototype)) {
    target[key] = val
    return val
  }
  if ((target as any)._isVue || (ob && ob.vmCount)) {
    __DEV__ &&
      warn(
        'Avoid adding reactive properties to a Vue instance or its root $data ' +
          'at runtime - declare it upfront in the data option.'
      )
    return val
  }
  if (!ob) {
    target[key] = val
    return val
  }
  defineReactive(ob.value, key, val, undefined, ob.shallow, ob.mock)
  if (__DEV__) {
    ob.dep.notify({
      type: TriggerOpTypes.ADD,
      target: target,
      key,
      newValue: val,
      oldValue: undefined
    })
  } else {
    ob.dep.notify()
  }
  return val
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does set() do?
set() is a function in the vue codebase, defined in src/core/observer/index.ts.
Where is set() defined?
set() is defined in src/core/observer/index.ts at line 223.
What does set() call?
set() calls 3 function(s): defineReactive, notify, observe.
What calls set()?
set() is called by 1 function(s): mergeData.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free