Skip to content
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

Open
Sunny-117 opened this issue Nov 3, 2022 · 15 comments
Open

instanceof #32

Sunny-117 opened this issue Nov 3, 2022 · 15 comments

Comments

@Sunny-117
Copy link
Owner

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
@faga295
Copy link

faga295 commented Nov 12, 2022

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

@23olddog
Copy link

23olddog commented Nov 19, 2022

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))

@veneno-o
Copy link
Contributor

veneno-o commented Jan 16, 2023

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);
	}
}

@hannah-bingo
Copy link
Contributor

<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>

@coffeeAndTeaa
Copy link

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

@bearki99
Copy link

bearki99 commented Feb 2, 2023

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

@sv-98-maxin
Copy link

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
}

@kangkang123269
Copy link

function myInstanceof(obj, constructor) {
  let proto = Object.getPrototypeOf(obj);
  while (proto) {
    if (proto === constructor.prototype) {
      return true;
    }
    proto = Object.getPrototypeOf(proto);
  }
  return false;
}

@LifeIsTerrible
Copy link

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);
    }
}

@Digoler
Copy link

Digoler commented Mar 3, 2023

 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)
        }
    }

@YMnotafraid
Copy link

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

@Bbbtt04
Copy link

Bbbtt04 commented Mar 12, 2023

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));

@xun-zi
Copy link

xun-zi commented Mar 13, 2023

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));

@cscty
Copy link

cscty commented Jun 14, 2023

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)
      }

@luckymore
Copy link

都遗漏了左侧必须是个 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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests