Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save e-jigsaw/6085330 to your computer and use it in GitHub Desktop.
Save e-jigsaw/6085330 to your computer and use it in GitHub Desktop.

coffeescript で undefined と書いたら void 0 に変換される

Coffeescript で

hoge = 0 if hoge is undefined

みたいなコードを書くと

if (hoge === void 0) {
  hoge = 0;
}

が出力されてて、そもそも void 0 ってナンジャラホイとおもったら

この演算子は、「戻り値が undefined であってほしい場所に、それ以外の戻り値を持つ式を挿入したい場合」に有用です。

http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/void

とのこと。ようするに undefined を返すだけの演算子らしい。え、じゃあ undefined でよくね?っておもったら、

なんと、undefined はただのグローバル変数

http://liginc.co.jp/web/js/38494

ええっ、まじで。

alert(undefined); // "undefined"
var undefined = "こんにちは";
alert(undefined) // "こんにちは"

http://liginc.co.jp/web/js/38494

まじかよ。

つーわけで、void 0 が使われるのね、なるほど。

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