New Vue
如果有看过源码的同学,不难发现从⼊⼝代码开始我们先来分析 new Vue 之后发⽣了哪些事情。
- new 关键字在 Javascript 语⾔中代表实例化是⼀个对象。⽽ Vue 实际上是⼀个类,类在 Javascript 中是⽤ Function 来实现的,来看⼀下源码,在
src/core/instance/index.js
中。
function Vue (options) {
if (process.env.NODE_ENV !== 'production'&& !(this instanceof Vue)){
warn('Vue is a constructor and should be called with the `new` keyword')
}
this._init(options)
}
可以看到 Vue 只能通过 new 关键字初始化,然后会调⽤ this._init ⽅法, 该⽅法在
src/core/instance/init.js
Vue.prototype._init