粥里有勺糖

vuePress-theme-reco 粥里有勺糖    2018 - 2023
粥里有勺糖 粥里有勺糖

Choose mode

  • dark
  • auto
  • light
关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
author-avatar

粥里有勺糖

285

文章

40

标签

关于我
备战春秋
  • 心得总结
  • 校招考点汇总
  • 面经汇总
  • 复习自查
技术笔记
  • 技术教程
  • 模板工程
  • 源码学习
  • 技术概念
  • 个人作品
  • 学习笔记
计算机基础
  • 算法与数据结构
  • 操作系统
  • 计算机网络
  • 设计模式
  • 剑指offer
大前端
  • javascript
  • vue
  • html
  • css
  • 🌏浏览器专题
  • Web性能优化
  • regexp
  • node
面试
  • 问解
  • javascript
  • css
  • 手撕代码
  • 性能优化
  • 综合问题
  • 面经汇总
  • 小程序
手撕代码
  • 数据结构与算法
  • javascript
  • css
个人站点
  • GitHub (opens new window)
  • 博客园 (opens new window)
  • 掘金 (opens new window)
线上作品
  • 轻取(文件收集) (opens new window)
  • 个人图床 (opens new window)
  • 考勤小程序 (opens new window)
  • 时光恋人 (opens new window)
  • 在线简历生成 (opens new window)
留言板
Github (opens new window)
  • other

    • 前端面试综合问题
    • 前端工程化
    • 在浏览器中输入URL到页面渲染的整个过程
    • typeof 是否能正确判断类型?instanceof 能正确判断对象的原理是什么?
    • 对象类型和原始类型的不同之处?函数参数是对象会发生什么问题?
    • 什么是提升?什么是暂时性死区?var、let 及 const 区别?
    • 如何理解原型?如何理解原型链?

typeof是否能正确判断类型?instanceof能正确判断对象的原理是什么?

vuePress-theme-reco 粥里有勺糖    2018 - 2023

typeof是否能正确判断类型?instanceof能正确判断对象的原理是什么?

粥里有勺糖 2020-03-10 面试其它

# typeof 是否能正确判断类型?instanceof 能正确判断对象的原理是什么?

# typeof

  • typeof对于原始值类型除了null外都可以准确判断
  • typeof 对于对象来说,除了函数都会显示 object

所以说 typeof 并不能准确判断变量到底是什么类型

typeof 1    // number
typeof '1'  // string
typeof true // boolean
typeof undefined // undefined
typeof Symbol // symbol
typeof null // object(实际上null不是Object)
typeof [] // object
typeof {} // object
typeof Function // function 
1
2
3
4
5
6
7
8
9

# instanceof

通过原型链来判断的

function People() { }
let p = new People()
console.log(p instanceof People) // true

function Teacher() { }
Teacher.prototype = Object.create(People.prototype)
function Student() { }
Student.prototype = Object.create(People.prototype)

let t = new Teacher()
console.log(t instanceof Teacher) // true
console.log(t instanceof People) // true
console.log(t instanceof Student) // false
1
2
3
4
5
6
7
8
9
10
11
12
13

# myInstanceof

function myInstanceof(target, origin) {
    let prototype = origin.prototype
    target = Object.getPrototypeOf(target)
    while (true) {
        if (target === null || target === undefined) {
            return false
        }
        if (target === prototype) {
            return true
        }
        target = Object.getPrototypeOf(target)
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
Edit this page (opens new window)
Last Updated: 2022/5/15 12:46:34