Skip to content

如何实现 instanceof 方法? #12

Open

Description

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

Activity

  1. changed the title [-]手写题:如何实现 instanceof 方法?[/-] [+]如何实现 instanceof 方法?[/+] on Jun 14, 2022
  2. RainyNight9 commented on Jun 14, 2022

    @RainyNight9
    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   
  3. luckept commented on Jun 14, 2022

    @luckept
    Member
    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

  4. luckept commented on Jun 14, 2022

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

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

  5. Hongbusi commented on Jun 14, 2022

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

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

  6. luckept commented on Jun 14, 2022

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

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

    棒!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions