手撕instanceof

ES5:

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__; 
 } 
}

ES6:

const instanceOf = (L, R) => L == null ? false : L == R ? true : instanceOf(L.__proto__, R)
 

发布了321 篇原创文章 · 获赞 48 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/ferrysoul/article/details/105597901
今日推荐