TypeScript

Vue 中 extend

// 必选参数to的类型,可选参数from的类型 冒号后是函数返回值的类型
export function extend (to: Object, _from: ?Object): Object {
  for (const key in _from) {
    to[key] = _from[key]
  }
  return to
}

Vue 中 use

export function initUse(Vue: GlobalAPI) {
  Vue.use = function (plugin: Function | Object) {
    const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))
    if (installedPlugins.indexOf(plugin) > -1) {
      return this
    }

    // additional parameters
    // 把数组中的第一次元素(plugin)去除
    const args = toArray(arguments, 1)
    // 把 this(Vue)插入第一个元素的位置
    args.unshift(this)
    if (typeof plugin.install === 'function') {
      plugin.install.apply(plugin, args)
    } else if (typeof plugin === 'function') {
      plugin.apply(null, args)
    }
    installedPlugins.push(plugin)
    return this
  }
}
``
posted @ 2020-07-22 10:30  荣光无限  阅读(116)  评论(0编辑  收藏  举报