javascript中的原型,原型链以及闭包(5)--------instanceof

首先我们讲讲instanceof的作用,其实它与typeof是近亲,怎末这么说呢·?
因为:它们的作用是大相径庭的;instanceof返回值为true or false,来判断对象的类型;typeof在这里就不多说了。

instanceof用法:object instanceof type
例子:

    var experement = {}
	console.log(experement instanceof Object);//true

然而,我们不能知其然而不知所以然。
如上例子:experement通过原型访问器沿着原型链走,同时Object通过属性prototype走,当二者能相遇,则返回true,否则返回false。

示例:

    console.log(Function instanceof Function);//true
	console.log(Object instanceof Object);//true
	console.log(Object instanceof Function);//true
	console.log(Function instanceof Object);//true

具体解释咱来看张图:
在这里插入图片描述
仔细分析,我们就要开始触摸原型链了,期待。。。。。

猜你喜欢

转载自blog.csdn.net/h1234561234561/article/details/86687875