You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functioninstance_of(L,R){//L 表示左表达式,R 表示右表达式varO=R.prototype;// 取 R 的显示原型L=L.__proto__;// 取 L 的隐式原型while(true){if(L===null)returnfalse;if(O===L)// 这里重点:当 O 严格等于 L 时,返回 true returntrue;L=L.__proto__;}}
答案:true true
The text was updated successfully, but these errors were encountered:
一、当初,为什么要为 JS 设计原型链?
当初,设计 Javascript 这门语言时,作者既想该语言拥有「万物皆对象」的优点,又不想加入类的概念来增加语言的复杂度。
因此,作者提出了原型链(prototype chain)的概念,用来「连接」对象,使多个对象可以共享同一份变量,让「继承」成为了可能。
二、__proto__、prototype、constructor 各自代表了什么?
__proto__
: 通常指向当前对象对应的构造函数中的prototype
,所有的对象都会有这个属性。这里的「通常」指的是「非JS内置的对象」。而对于「JS内置的对象」,我建议:「硬记」~
prototype
: 这就是所谓的原型,只有构造函数才有该属性。constructor
: 生成当前对象的构造函数,所有对象都有构造函数。相较于那些枯燥的等式,结合下图,或许能够更快的理清各种的连接关系。
三、JS 对象的寻值逻辑
四、几个衍生出来的面试题
New
关键字功能类似的NEW
方法 ?解释:
instanceof
除了会判断对象本身的类型,还会判断对象的__proto__
的类型,其伪码大致如下:答案:
true true
The text was updated successfully, but these errors were encountered: