在 src/core/instance/index.js 中
if (process.env.NODE_ENV !== 'production' &&
!(this instanceof Vue)
) {
warn('Vue is a constructor and should be called with the `new` keyword')
}
先说结论:这里通过 this instanceof Vue 来判断有没有用 new 关键词调用。
那么,为什么可以这么判断呢?我们分别了解一下 this 和 instanceof 的用法。
this
在 JavaScript 中,this 是动态绑定,或称为运行期绑定的,它可以是全局对象、当前对象或者任意对象,这取决于函数的调用方式。函数的调用有以下几种方式:作为对象方法调用,作为函数调用,作为构造函数调用,和使用 apply 或 call 调用。
具体的使用方式请移步至:浏览器 JavaScript 中的 this 的使用,具体我就不展开赘述了。
instanceof
instanceof 有几个用法:
1、通常使用 instanceof 就是判断一个实例是否属于某种类型,比如:
// 判断 foo 是否是 Foo 类的实例
function Foo(){}
var foo = new Foo();
console.log(foo instanceof Foo)//true
2、更重要的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。例如:
// 判断 foo 是否是 Foo 类的实例 , 并且是否是其父类型的实例
function Aoo(){}
function Foo(){}
Foo.prototype = new Aoo(); // JavaScript 原型继承
var foo = new Foo();
console.log(foo instanceof Foo) // true
console.log(foo instanceof Aoo) // true
上面的代码中是判断了一层继承关系中的父类,在多层继承关系中,instanceof 运算符同样适用。
3、ECMAScript 中 instanceof 的定义
11.8.6 The instanceof operator The production RelationalExpression:
RelationalExpression instanceof ShiftExpression is evaluated as follows:
1. Evaluate RelationalExpression.
2. Call GetValue(Result(1)).// 调用 GetValue 方法得到 Result(1) 的值,设为 Result(2)
3. Evaluate ShiftExpression.
4. Call GetValue(Result(3)).// 同理,这里设为 Result(4)
5. If Result(4) is not an object, throw a TypeError exception.// 如果 Result(4) 不是 object,
//抛出异常
/* 如果 Result(4) 没有 [[HasInstance]] 方法,抛出异常。规范中的所有 [[...]] 方法或者属性都是内部的,
在 JavaScript 中不能直接使用。并且规范中说明,只有 Function 对象实现了 [[HasInstance]] 方法。
所以这里可以简单的理解为:如果 Result(4) 不是 Function 对象,抛出异常 */
6. If Result(4) does not have a [[HasInstance]] method,
throw a TypeError exception.
// 相当于这样调用:Result(4).[[HasInstance]](Result(2))
7. Call the [[HasInstance]] method of Result(4) with parameter Result(2).
8. Return Result(7).
// 相关的 HasInstance 方法定义
15.3.5.3 [[HasInstance]] (V)
Assume F is a Function object.// 这里 F 就是上面的 Result(4),V 是 Result(2)
When the [[HasInstance]] method of F is called with value V,
the following steps are taken:
1. If V is not an object, return false.// 如果 V 不是 object,直接返回 false
2. Call the [[Get]] method of F with property name "prototype".// 用 [[Get]] 方法取
// F 的 prototype 属性
3. Let O be Result(2).//O = F.[[Get]]("prototype")
4. If O is not an object, throw a TypeError exception.
5. Let V be the value of the [[Prototype]] property of V.//V = V.[[Prototype]]
6. If V is null, return false.
// 这里是关键,如果 O 和 V 引用的是同一个对象,则返回 true;否则,到 Step 8 返回 Step 5 继续循环
7. If O and V refer to the same object or if they refer to objects
joined to each other (section 13.1.2), return true.
8. Go to step 5.
翻译成 JavaScript 代码如下所示:
function instance_of(L, R) { // L 表示左表达式,R 表示右表达式
var O = R.prototype; // 取 R 的显示原型
L = L.__proto__; // 取 L 的隐式原型
while (true) {
if (L === null)
return false;
if (O === L) // 这里重点:当 O 严格等于 L 时,返回 true
return true;
L = L.__proto__;
}
}
从代码中我们可以看到,instanceof 是比较左侧的 __proto__ (隐式原型)和右侧的 prototype (显示原型)是否相等,如果不相等,取左侧 __proto__ 的 __proto__ ,依次循环比较,直到取到 Object.prototype.__proto__ 即 null 为止。
关于 __proto__ 和 prototype 请移步至:原型与原型链
回到主题
this instanceof Vue 我们可以这么分解:this.__proto__ 和 Vue.prototype
-
没有使用
newthis指向window,结果为false。 -
使用了
new回到上面作为构造函数调用:- 第一步:创建一个空的对象,
var o = {}。 - 第二步:链接该对象(即设置该对象的构造函数)到另一个对象,即
o.__proto__ == Vue.prototype。 - 第三步:将步骤1新创建的对象作为
this的上下文 - 第四步:如果该函数没有返回对象,则返回
this
所以,结果可以看做这样:
o.__proto__ == this.__proto__ == Vue.prototype - 第一步:创建一个空的对象,
所以如果用 new 操作符的话,this instanceof Vue 结果为 true。