Skip to content

Instantly share code, notes, and snippets.

@shhider
Created September 12, 2017 03:11
Show Gist options
  • Save shhider/e816791a4c6f8bd12f6aaffd3605a29c to your computer and use it in GitHub Desktop.
Save shhider/e816791a4c6f8bd12f6aaffd3605a29c to your computer and use it in GitHub Desktop.

1、

if (x === undefined) { ... }

不严谨,undefined 关键字是可以被更改的。

2、

if (x === void 0) { ... }

严谨。但如果变量 x 没有被声明过,是会报 ReferenceError 的错误的;另外可读性也不算好。

3、

if (typeof x === 'undefined') { ... }

严谨。虽然从原理上来讲,性能上会差一些,不过在引擎的优化下,性能与上述两种方法已经基本一致了,所以本方法最推荐。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment