V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
nodejsexpress
V2EX  ›  Node.js

怎样才能看到变量的真实类型?

  •  
  •   nodejsexpress · Nov 7, 2023 · 1881 views
    This topic created in 901 days ago, the information mentioned may be changed or developed.

    js 里面一切都是 object ,有什么方法是可以细化到具体的类型吗?

    > let arr = [1,2,3,4]
    undefined
    > typeof arr
    'object'
    > let mapper = {'name':'dog'}
    undefined
    > typeof mapper
    'object'
    >
    

    因为在其他语言,比如 python ,object 是专指 class 一类的对象, dict ,list ,tuple 等各种类型。

    有时候调试挺累的,用 typeof

    3 replies    2023-11-29 12:11:25 +08:00
    6379616e
        1
    6379616e  
       Nov 7, 2023   ❤️ 1
    这个不难查到吧 Object.prototype.toString.call(target)
    nodejsexpress
        2
    nodejsexpress  
    OP
       Nov 7, 2023
    @6379616e 感谢大神指点
    kurax
        3
    kurax  
       Nov 29, 2023
    判断数组可用 Array.isArray()
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3590 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 04:42 · PVG 12:42 · LAX 21:42 · JFK 00:42
    ♥ Do have faith in what you're doing.