extend方法中子类的extend属性已经指向了父类的extend,那为什么extend要作为一个静态方法,而不直接挂在Vue.prototype上呢?

来源:3-2 createComponent

王王王博

2019-08-23

const Sub = function VueComponent (options) {
  this._init(options)
}
Sub.prototype = Object.create(Super.prototype)
Sub.prototype.constructor = Sub
Sub.cid = cid++
Sub.options = mergeOptions(
  Super.options,
  extendOptions
)
Sub['super'] = Super

// For props and computed properties, we define the proxy getters on
// the Vue instances at extension time, on the extended prototype. This
// avoids Object.defineProperty calls for each instance created.
if (Sub.options.props) {
  initProps(Sub)
}
if (Sub.options.computed) {
  initComputed(Sub)
}

// allow further extension/mixin/plugin usage
Sub.extend = Super.extend
Sub.mixin = Super.mixin
Sub.use = Super.use

(发现看视频的页面提问的输入框只能往左后退一步<-,不能->前进,bug吗,复制也不能?)

写回答

1回答

王王王博

提问者

2019-08-23

是为了避免被vm实例访问吗?

0
1
ustbhuangyi
extend 的目的就是返回一个构造器,和实例无关,没必要挂载到原型上吧
2019-08-23
共1条回复

Vue.js 源码深入解析 深入理解Vue实现原理

全方位讲解 Vue.js 源码,进阶高级工程师

4984 学习 · 1037 问题

查看课程