javascript如何判断一个变量未定义

undefined

永远不要直接使用undefined进行变量判断;

使用typeof和字符串'undefined'对变量进行判断。

// not good
if (person === undefined) {
    ...
}

// good
if (typeof person === 'undefined') {
    ...
}

摘自腾讯的代码规范文档
http://alloyteam.github.io/CodeGuide/index.html
http://alloyteam.github.io/Co...

评论

暂无评论