# All files / src/instance props.ts

92% Statements 23/25
50% Branches 6/12
100% Functions 3/3
92% Lines 23/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    1x
    1x
    1x
    1x

    2x
    2x
    2x
    2x
    2x

    2x

    2x

    1x

    2x
    2x

    2x
    2x

    2x

    1x

    2x

    2x
    2x

    2x

    2x

     import { hasOwn } from '../util/hasOwn'
    import type { PropOptions } from '../types/options'

    const NULL_PROP = null

    export function initProps (vm, propsOptions: object): WechatMiniprogram.Component.PropertyOption {
      // miniprogram props
      const properties = {}
      for (const key in propsOptions) {
        const propOptions = propsOptions[key]
        const type = propOptions.type || NULL_PROP
        const value = validateProp(key, propsOptions, {}, vm)
        // 属性的类型（可以指定多个）
        properties[key] = Array.isArray(type)
          ? { optionalTypes: type, type, value }
          : { type, value }
      }
      return properties
    }

    export function validateProp (key: string, propsOptions, propsData, vm) {
      const prop = propsOptions[key]
      let value = propsData[key]
      // check default value
      ifE (value === undefined) {
        value = getPropDefaultValue(vm, prop, key)
      }
      return value
    }

    function getPropDefaultValue (vm, prop: PropOptions, key: string) {
      // no default, return null
      ifI (!hasOwn(prop, 'default')) {
        return NULL_PROP
      }
      const def = prop.default
      let value = typeof def === 'function' ? def.call(vm) : def
      // fix undefined for miniprogram
      ifI (value === undefined) {
        value = NULL_PROP
      }
      return value
    }