-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
instanceof #32
Comments
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 _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> |
this is super elegant |
|
function myInstanceof(obj, constructor) {
let proto = Object.getPrototypeOf(obj);
while (proto) {
if (proto === constructor.prototype) {
return true;
}
proto = Object.getPrototypeOf(proto);
}
return false;
} |
|
|
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)); |
|
都遗漏了左侧必须是个 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); |
function myInstanceof(left, right) {
if (typeof left !== "object" || left === null) {
return false;
}
let proto = Object.getPrototypeOf(left);
while (proto) {
if (proto === right.prototype) {
return true;
}
proto = Object.getPrototypeOf(proto);
}
return false;
}
|
function MyInstanceof(obj, constructorFunc){
const _proto_ = Object.getPrototypeOf(obj);
if(_proto_){
if(_proto_ === constructorFunc.prototype){
return true
}
return MyInstanceof(_proto_, constructorFunc)
}
else{
return false
}
} |
const myInstanceOf = (left, right) => { console.log(myInstanceOf([], Array)); |
The text was updated successfully, but these errors were encountered: