在src/core/instance/index.js中app
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的用法函数
在 JavaScript 中,this 是动态绑定,或称为运行期绑定的,它能够是全局对象、当前对象或者任意对象,这取决于函数的调用方式。函数的调用有如下几种方式:做为对象方法调用,做为函数调用,做为构造函数调用,和使用 apply 或 call 调用。this
一、做为对象方法调用lua
var point = { x : 0, y : 0, moveTo : function(x, y) { this.x = this.x + x; this.y = this.y + y; } };
point.moveTo(1, 1)//this 绑定到当前对象,即 point 对象prototype
二、做为函数调用翻译
function makeNoSense(y) { this.x = y; } makeNoSense(5); x;// 调用函数的对象是window,因此x 已经成为一个值为 5 的全局变量
下面,咱们看另外一种状况code
var point = { x : 0, y : 0, moveTo : function(x, y) { // 内部函数 var moveX = function(x) { this.x = x;//this 绑定到了哪里? }; // 内部函数 var moveY = function(y) { this.y = y;//this 绑定到了哪里? }; moveX(x); moveY(y); } }; point.moveTo(1, 1); point.x; //==>0 point.y; //==>0 x; //==>1 y; //==>1
this除了指向它的直接调用者外,还有一种状况就是若是没有明确的调用对象的时候,将对函数的this使用默认绑定:绑定到全局的window对象。对象
三、做为构造函数调用继承
function Point(x, y){ this.x = x; this.y = y; } var test = new Point(1, 2)
咱们须要理解的是,new运算符作了什么:ip
第一步: 建立一个空的对象,{}。 第二步: 连接该对象(即设置该对象的构造函数)到另外一个对象,即o.\__proto__ == Point.prototype。 第三步: 将步骤1新建立的对象做为this的上下文 第四步: 若是该函数没有返回对象,则返回this
四、使用 apply 或 call 调用
apply和call能够切换函数执行的上下文环境(context)
function add(x, y) { console.log(x + y) } function del(x, y) { console.log(x - y) } add.call(del, 3, 1) // 4
一、一般来说,使用 instanceof 就是判断一个实例是否属于某种类型,好比:
// 判断 foo 是不是 Foo 类的实例 function Foo(){} var foo = new Foo(); console.log(foo instanceof Foo)//true
二、另外,更重要的一点是 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 运算符一样适用。
三、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
this指向window,结果为false。
回到上面做为构造函数调用:
第一步: 建立一个空的对象,vat o = {}。 第二步: 连接该对象(即设置该对象的构造函数)到另外一个对象,即o.\__proto__ == Vue.prototype。 第三步: 将步骤1新建立的对象做为this的上下文 第四步: 若是该函数没有返回对象,则返回this
因此,结果能够看作这样:
o.\__proto__ == this.\__proto__ == Vue.prototype
因此若是用new操做符的话,this instanceof Vue结果为true。