Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(operator): 各演算子の解説を追加 #104

Merged
merged 34 commits into from
Jul 30, 2016
Merged

feat(operator): 各演算子の解説を追加 #104

merged 34 commits into from
Jul 30, 2016

Conversation

azu
Copy link
Collaborator

@azu azu commented Jul 25, 2016

#32 #101 の解説を追加

演算子は検索しにくいので、このページにまとめるという方針で書いています。
なので、若干リファレンスチックになっていますが、この節が特別という感じです。

全体的にざっくりな解説にしてる。

```js
8 % 3; // => 2
```

### 指数演算子 (`**`)
### [ES2016] べき乗演算子 (`**`)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

べき乗演算子のSyntax Errorについては正直これ使う人なら検索するでしょという感じなので、書いてない。

```

JavaScriptでは`0`もif文では`false`として扱われます。
そのため、`~indexOfの結果`が`0`となることを利用して書くイディオムが一部では使われています。
Copy link
Collaborator Author

@azu azu Jul 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~indexOfの結果

このイディオムは意義は短く書ける + 左オペランドということなので、
includes がある今は殆ど使う必要すらないという認識なんだけど実際どうなんだろ。
(そもそも、一見で読めないので === -0 といつも書いてる)

```

デフォルト引数は`prefix`が未定義(`undefined`)である時のみ、
デフォルト値を代わりに代入してくれる機能であるため、OR演算子のような意図しない挙動を避けることができます。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代わりに代入

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

デフォルト引数とは、関数呼び出しの際に指定した引数が省略された時(正確にはundefinedの時)、代わりに引数として渡してくれる機能です。先に上げたようなOR演算子の問題を回避できます


みたいなかんじでしょうか?

ところで

function f(){
    return 3;
}
function g(n = f()){
    //do something
}
g();//OK

も合法なんですね。ようはデフォルト引数を引数に渡すときに評価できる式ならいいのか。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

次のような雰囲気の処理なので、

n = (n !== undefeind) ? n : f()

これを利用して必須の引数がなかったら例外を投げるみたいなこともできたりしますね。
(現実的には微妙ですが。。JSDocとかTypeScript使ってどうにかした方がいい気はする)

Copy link
Contributor

@yumetodo yumetodo Jul 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これを利用して必須の引数がなかったら例外を投げるみたいなこともできたりしますね。

そのへんは関数のoverloadでどうにかしているC++erとしては

void f(int n){
    //do something
}
void f(...){
    static_assert(false, "In function `f` : Invaid arguments.");
}
int main(){
    f();//compile error : In function `f` : Invaid arguments.
    f("string");//compile error : In function `f` : Invaid arguments.
    f(3);//OK
}

ものすごく斬新なデフォルト引数の使い方でした・・・。

JSDocとか

ええっと、JSDocってドキュメント書くものだと思ってたんですがどうやってチェックするんでしょう?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ものすごく斬新なデフォルト引数の使い方でした・・・。

実際に使ってるところはまだ見たことないです…
(こういうのは開発中のみチェックしたいと思うので、取り除きにくそうで微妙なやり方だなーと思った感じです)

JSDoc

言語機能としては全く関係なくツールレベルの話なのでアレですが。
最近のエディタはJSDocぐらいは解釈するのでエディタ機能としてのチェックしたり、
後は、JSDocをランタイムassertに変換して実行するものを使ったりして、必須引数ぐらいならチェックできそうな感じですね。
(ちゃんとやるなら、普通にassertライブラリを使って手動でやるべきですが)

http://efcl.info/2016/03/25/jsdoc-to-assert/

@azu
Copy link
Collaborator Author

azu commented Jul 28, 2016

inとかnewとかdeleteみたいなやつはこのくくりで無理やり書かなくてもいいかなー
演算子の記号編みたいなくくりで良さそうな気がする。
(1ページに詰めて書いてるのは、演算子は検索しにくいので一箇所にまとめておこうみたいなのがある)

@azu azu changed the title WIP: feat(operator): 各演算子の解説を追加 feat(operator): 各演算子の解説を追加 Jul 28, 2016
console.log(c); // => 3
```

カンマ演算子を活用したテクニックとして[indirect call][]というものがあります。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indirect function callの話 一応触れてるけど、現実的に自分で書くことはまずない

@@ -746,22 +746,29 @@ a + b * c; // 7
```

[演算子の優先順序][]は仕様で定義されていますが、多様な演算子が出てきた場合に見分けるのは難しいです。
そのため、グループ演算子を使い優先順序を明示することが読みやすいコードへとつながります。
グループ演算子はもっとも優先度が高い演算子となります。
そのため、グループ演算子使い優先順序を明示することが読みやすいコードへとつながります。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必ずしもそうではないはず

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね。
https://twitter.com/azu_re/status/759268034276159492
()をなしの例を書こうしてたのが残ってました。
(これは具体的な例に左右されそうなきがするので削除します)

@azu
Copy link
Collaborator Author

azu commented Jul 30, 2016

ひとまず書けたのでマージします。
細かいのはページとしてみてから直していく

@azu azu merged commit 7bbd6e5 into master Jul 30, 2016
@azu azu deleted the operator-intro branch July 30, 2016 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants