Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

instanceof

Sunny-117 opened this issue · comments

commented
function myInstanceof(left, right) {
  let proto = Object.getPrototypeOf(left), // 获取对象的原型
      prototype = right.prototype; // 获取构造函数的 prototype 对象

  // 判断构造函数的 prototype 对象是否在对象的原型链上
  while (true) {
    if (!proto) return false;
    if (proto === prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
}
function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object));
// console.log(p instanceof Person);//true
commented
function myInstanceof(left, right) {
  if (!left) return false;
  return (
    left.__proto__ === right.prototype || myInstanceof(left.__proto__, right)
  );
}
function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object));
// console.log(p instanceof Person);//true
function myInstanceof(left = null, right) {
    // 对于左侧参数如果是非对象直接返回false
    if (Object(left) !== left) return false
    // 对于右侧参数可以认为只能为函数且不能没有Prototype属性
    if (typeof right !== 'function' || !right.prototype) throw new TypeError('Right-hand side of 'instanceof' is not an object')
    // 声明一个变量获取对象的__proto__
    let link = left.__proto__
    // 做循环(当link最终指向null,如果指向null的情况下都找不到那就返回false)
    while (link !== null) {
        // 如果找到说明R.prototype在L的原型链上,即返回true
        if(link === right.prototype) return true
        // 逐级向下
        link = link.__proto__
    }
    return false
}
//编写测试用例
function Person(name, age) {
    this.name = name;
    this.age = age;
  }
  function School(name, address) {
    this.name= name;
    this.address = address;
  }
  const p1 = new Person('james', 23);
  console.log(myInstanceof(p1, Person) + '--' + myInstanceof(p1, Object) + '--' + myInstanceof(p1, School))
function _instanceof(obj, cla) {
	let objProto = Object.getPrototypeOf(obj);
	const claProto = cla.prototype;

	while (true) {
		if (objProto === null) {
			return false;
		}
		if (objProto === claProto) {
			return true;
		}
		objProto = Object.getPrototypeOf(objProto);
	}
}
<script>
		function _instanceOf(left, right) {
			const rightProto = right.prototype;
			left = left.__proto__
			while(left.__proto__ !== null) {
				if(rightProto === left) {
					return true;
				}
				left = left.__proto__;
			}
			return false
		}
		// 测试用例
		function Person () {}
		function Person2 () {}
		const no = new Person()
		const no2 = new Person2()
		console.log(_instanceOf(no2, Person))
		console.log(_instanceOf(no, Person))
	</script>
commented
function myInstanceof(left, right) {
  if (!left) return false;
  return (
    left.__proto__ === right.prototype || myInstanceof(left.__proto__, right)
  );
}
function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object));
// console.log(p instanceof Person);//true

this is super elegant

commented
function myInstance(obj, proto) {
  let proto1 = Object.getPrototypeOf(obj);
  let proto2 = proto.prototype;
  while (1) {
    if (proto1 == null) return false;
    if (proto1 == proto2) return true;
    proto1 = Object.getPrototypeOf(proto1);
  }
}

注:尽量使用Object.getPrototypeOf获取对象的原型
image

function instanceOf(left, right) {
  let a = Object.getPrototypeOf(left), b = right.prototype;
  while (a) {
    if (a === b) {
      return true
    }
    a = Object.getPrototypeOf(a)
  }
  return false
}
function myInstanceof(obj, constructor) {
  let proto = Object.getPrototypeOf(obj);
  while (proto) {
    if (proto === constructor.prototype) {
      return true;
    }
    proto = Object.getPrototypeOf(proto);
  }
  return false;
}
function _instanceof(left, right) {
    let proto = Object.getPrototypeOf(left),
        prototype = right.prototype;
    while (true) {
        if (!proto) return false;
        if (proto === prototype) return true;
        // 查找原型链
        Object.getPrototypeOf(proto);
    }
}
 const myInstanceof = (left, right) => {
     let proto = Object.getPrototypeOf(left)
     while (true) {
         if (proto === null) return false
         if (proto === right.prototype) return true
         proto = Object.getPrototypeOf(proto)
        }
    }
commented
function myinstanceof(left, right) {
  let lpro = left.__proto__; //Object.getPrototypeOf(left)
  let rpro = right.prototype;
  while (lpro) {
    if (lpro === rpro) return true;
    lpro = lpro.__proto__;
  }
  return false;
}

console.log(myinstanceof(Function, Function)); //true
console.log([] instanceof Array);

function myInstanceof(left, right) {
  let protoLeft = Object.getPrototypeOf(left)
  let protoRight = right.prototype

  while (true) {
    if (protoLeft === null) return false
    if (protoLeft === protoRight) return true
    protoLeft = Object.getPrototypeOf(left)
  }
}
console.log(myInstanceof([], Array));
function myInstanceof(left, right) {
        let proto = left.__proto__;
        const prototypee = right.prototype;
        while (true) {
            if (!proto) return false;
            if (proto == prototypee) return true;
            proto = proto.__proto__;
        }
    }
    function Person() { };
    var p = new Person();
    console.log(myInstanceof(p, Object));
commented
function instanceOf(son, father) {
        if (Object.getPrototypeOf(son) === null) return false
        if (father.prototype === Object.getPrototypeOf(son)) return true
        else return instanceOf(Object.getPrototypeOf(son), father)
      }

都遗漏了左侧必须是个 object 类型 1 instanceof Number === false

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.

function myInstanceof(left, right) {
  if (typeof left !== 'object') return false
  let up = Object.getPrototypeOf(left)
  while (up) {
    if (up === right.prototype) {
      return true
    }
    up = Object.getPrototypeOf(up)
  }
  return false
}

function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object), p instanceof Object);
console.log(myInstanceof(1, Number), 1 instanceof Number);