javascript instanceof

javascript instanceof分析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__; 
  } 
 }

转自:http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/java

(感谢做者分享)web

因而可知 inA instanceof funcB 比较的是prototype

实例inA的隐式原型链的对象跟funcB的原型比较,所以code

function Myobj(){}
		var obj1 = new Myobj();
		Myobj.prototype = {
			constructor:Myobj
		}

		var obj2 = new Myobj();

		console.log(obj1 instanceof Myobj);//false
		console.log(obj2 instanceof Myobj);//true

		console.log(obj1.constructor === obj2.constructor);//true

		console.log(obj1.constructor === obj2.constructor);//true

结果一目了然对象

相关文章
相关标签/搜索