构造函数、实例、原型对象之间的关系
检测对象的原型对象与构造函数的原型是否是同一个对象,如果不是,根据对象的原型链依次查找,直到相等,或者到达原型链顶端。
上面文字可能表达的不是很清楚,下面上代码:
function myinstanceof (obj, constructor) { // 对象的原型对象 let objproto = object.getprototypeof(obj); // 构造函数的原型对象 let constructorproto = constructor.prototype; if (objproto === constructorproto) { return true; } else { while (objproto = object.getprototypeof(objproto)) { if (objproto === constructorproto) { return true; } } return false; } } function person (name) { this.name = name; } person.prototype.hi = function () { console.log(`hi, my name is ${this.name}`); }; let p = new person('monkey'); p.hi(); console.log('------------------------------------------------'); let other = {name: 'lucy'}; console.log(`p is myinstanceof person : ${myinstanceof(p, person)}`); console.log(`p is myinstanceof object : ${myinstanceof(p, object)}`); console.log(`other is myinstanceof person : ${myinstanceof(other, person)}`); console.log(`other is myinstanceof object : ${myinstanceof(other, object)}`); console.log('------------------------------------------------'); console.log(`p is instanceof person : ${myinstanceof(p, person)}`); console.log(`p is instanceof object : ${myinstanceof(p, object)}`); console.log(`other is instanceof person : ${myinstanceof(other, person)}`); console.log(`other is instanceof object : ${myinstanceof(other, object)}`);
使用myinstanceof和instanceof的结果是一致的
如对本文有疑问, 点击进行留言回复!!
asp.net core 连接 docker apollo 获取不到数据,返回null
asp.net中ajax和一般处理程序(handler.ashx)的交互
egg.js创建项目,目录介绍,简单使用,sequelize mysql使用
网友评论