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 方法? #12

Open
Hongbusi opened this issue Jun 13, 2022 · 5 comments
Open

如何实现 instanceof 方法? #12

Hongbusi opened this issue Jun 13, 2022 · 5 comments

Comments

@Hongbusi
Copy link
Member

Hongbusi commented Jun 13, 2022

function _instanceof(leftValue, rightValue) {
  const leftValueType = typeof leftValue
  if (leftValueType !== 'object' && leftValueType !== 'function') return false

  rightValue = rightValue.prototype
  leftValue = leftValue.__proto__
  while (true) {
    if (leftValue === null) return false
    if (leftValue === rightValue) return true
    leftValue = leftValue.__proto__
  }
}

// 测试代码
console.log(_instanceof(Object, Function))
console.log(_instanceof(Function, Object))
console.log(_instanceof(Function, Function))
console.log(_instanceof(new Number(123), Number))
console.log(_instanceof(123, Number))

console.log(Object instanceof Function)
console.log(Function instanceof Object)
console.log(Function instanceof Function)
console.log(new Number(123) instanceof Number)
console.log(123 instanceof Number)
@Hongbusi Hongbusi added handwritten-code today 每日一题。 labels Jun 13, 2022
@Hongbusi Hongbusi changed the title 手写题:如何实现 instanceof 方法? 如何实现 instanceof 方法? Jun 14, 2022
@RainyNight9
Copy link
Member

function myInstanceof(left, right) {
  // 这里先用 typeof 来判断基础数据类型,如果是,直接返回 false
  if (typeof left !== 'object' || left === null) return false;
  // getPrototypeOf 是 Object 对象自带的 API,能够拿到参数的原型对象
  let proto = Object.getPrototypeOf(left);
  // 循环往下寻找,直到找到相同的原型对象
  while (true) {
    if (proto === null) return false;
    // 找到相同原型对象,返回true
    if (proto === right.prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
}
// 验证一下自己实现的 myInstanceof 是否 OK
console.log(myInstanceof(new Number(123), Number));    // true
console.log(myInstanceof(123, Number));                // false   

@luckept
Copy link
Member

luckept commented Jun 14, 2022

function _instanceof(leftValue, rightValue) {
  rightValue = rightValue.prototype
  leftValue = leftValue.__proto__
  while (true) {
    if (leftValue === null) return false
    if (leftValue === rightValue) return true
    leftValue = leftValue.__proto__
  }
}

洪总这里有个小问题
91f1720dd84727f2f659b2905006b31

@luckept
Copy link
Member

luckept commented Jun 14, 2022

function myInstanceof(left, right) {
  // 这里先用 typeof 来判断基础数据类型,如果是,直接返回 false
  if (typeof left !== 'object' || left === null) return false;
  // getPrototypeOf 是 Object 对象自带的 API,能够拿到参数的原型对象
  let proto = Object.getPrototypeOf(left);
  // 循环往下寻找,直到找到相同的原型对象
  while (true) {
    if (proto === null) return false;
    // 找到相同原型对象,返回true
    if (proto === right.prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
}
// 验证一下自己实现的 myInstanceof 是否 OK
console.log(myInstanceof(new Number(123), Number));    // true
console.log(myInstanceof(123, Number));                // false   

老哥这么写也有点小问题
2ef0910f47e837c1604485b8528f766

@Hongbusi
Copy link
Member Author

Hongbusi commented Jun 14, 2022

const leftValueType = typeof leftValue
if (leftValueType !== 'object' && leftValueType !== 'function') return false

增加了一个判断来确定 leftValue 值的类型。 @luckept

@luckept
Copy link
Member

luckept commented Jun 14, 2022

const leftValueType = typeof leftValue
if (leftValueType !== 'object' && leftValueType !== 'function') return false

增加了一个判断来确定 leftValue 值的类型。 @luckept

棒!

@Hongbusi Hongbusi removed the today 每日一题。 label Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants