【问题标题】:Does node.js shell have commands like python type? [duplicate]node.js shell 是否有类似 python 类型的命令? [复制]
【发布时间】:2013-06-13 08:47:28
【问题描述】:

我想知道一个对象是 null 还是 undefined,所以我使用以下代码:

if(obj==='undefined'||obj===null)

但它似乎不起作用。是否有类似的 python type 命令来获取 node.js shell 中 obj 的类型?谢谢!

【问题讨论】:

  • 我猜你在找typeof:typeof(foo) --> "undefined"
  • 确实如此。 obj==='undefined' 假定 obj 是一个值为 undefined 的字符串。
  • Underscore.js 有一些方便的函数来检查变量类型(isNullisUndefined 在你的情况下),但当然没有什么可以替代学习 Javascript :)。
  • 在python中你也不应该使用type,你应该使用isinstance

标签: javascript jquery python node.js shell


【解决方案1】:
> typeof foo == 'undefined'
true
> typeof 1 == 'number'
true

这应该适合你:

if( typeof obj === 'undefined' || obj === null)

来自docs

typeof 运算符返回一个字符串,指示 未计算的操作数。

【讨论】:

    猜你喜欢
    • 2015-05-15
    • 1970-01-01
    • 2012-07-08
    • 2021-07-30
    • 2015-05-20
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多