Giter Site home page Giter Site logo

notes's People

Contributors

cheungkin24 avatar

Watchers

 avatar

notes's Issues

instanceof的实现及原型的延伸

instanceof是什么

在JavaScript里,想判断一个变量是什么类型时,会使用typeof,在使用typeof时无论是什么类型的对象,都只会返回object,instanceof的作用与typeof相似,且instanceof方法可以返回对象究竟是什么对象。
以下为instanceof的JavaScript实现代码

function instance_of(L, R) {
    R = R.prototype
    L = L.__proto__
    while(true) {
      if (L === null) {
        return false
      }
      if (L === R) {
        return true
      }
      L === L.__proto__
    }
}

原型

构造函数都有一个prototype属性指向自己的原型对象,而通过构造函数生成的实例就有一个属性__proto__指向这个原型对象

用 Function.prototype.bind 创建的函数对象没有 prototype 属性

Function.prototype === Function.[[proto]]

typeof Function.prototype // function

image

常规用法

function Parent () {}
function Child () {}
Child.prototype === new Parent()
const child = new Child()
child instanceof Parent //true
child instanceof Child //true

进阶实现

  1. Object instanceof Object
// 第一次循环
L === Object.__proto__ // 也就是等于Function.prototype
R === Object.prototype // 也就是等于Object.prototype
第二次循环
L === Function.prototype.__proto__ // 也就是等于Object.prototype
R === Object.prototype
// 结果为true
  1. Function instanceof Function
// 第一次循环
L === Function.__proto__ // 相当于Function.prototype
R === Function.prototype
// 结果为true,因为Function.__proto__ === Function.prototype结果为true
  1. Number instanceof Number
// 第一次循环
L === Number.__proto__ // 相当于Function.prototype
R === Number.prototype
// 第二次循环
L === Function.prototype.__proto__ // 相当于Object.prototype
// 第三次循环
L === Object.prototype.__proto__ // null
// 结果为false

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.