什么'设计用于检查类型的代码是错误的

What's wrong with code designed to check type

本文关键字:类型 代码 错误 检查 用于 什么      更新时间:2024-05-24

我有一个带有regexp的函数,它应该检测应用对象的类型:Undefine|Null:

var isUndefinedOrNull = function(obj) {
    return /^(?:undefined|null)$/.test(typeof obj);
};

这个代码出了什么问题?

唯一的问题是typeof为空是object而不是'null'

我认为你可以使用

var isUndefinedOrNull = function (obj) {
    return obj === undefined || obj === null;
};
  • 的类型
  • 数据结构
相关文章: