美文网首页
typeof和instanceof的作用和区别?

typeof和instanceof的作用和区别?

作者: 张亚群 | 来源:发表于2018-11-29 18:04 被阅读8次

    typeof的作用:

    typeof :是一元运算符,返回值为字符串,该字符串用来说明运算数的数据类型

    • (1)原始类型
      数值、字符串、布尔值分别返回number、string、boolean
      (2)函数
      函数返回function
      (3)undefined
      undefined返回undefined。


      image.png

    instanceof的作用

    instanceof用于判断某个变量是否是某个对象的实例,返回值为true或false
    如果我们希望获取一个对象是否是数组,或判断某个变量是否是某个对象的实例则要选择使用instanceof;

    • 语法:object instanceof constructor
    • 参数:object要检测的对象.
      constructor某个构造函数


      image.png

    相关文章

      网友评论

          本文标题:typeof和instanceof的作用和区别?

          本文链接:https://www.haomeiwen.com/subject/zagfcqtx.html