设计用于检查类型的代码有什么问题

时间:2015-02-26 11:03:16

标签: javascript regex

我有regexp的函数,它应该检测类型:Undefined |应用对象的空:

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

这段代码出了什么问题?

1 个答案:

答案 0 :(得分:3)

唯一的问题是typeof null object而非'null'

我认为你可以使用

var isUndefinedOrNull = function (obj) {
    return obj === undefined || obj === null;
};
相关问题