-
-
Notifications
You must be signed in to change notification settings - Fork 224
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
Conversation
```js | ||
8 % 3; // => 2 | ||
``` | ||
|
||
### 指数演算子 (`**`) | ||
### [ES2016] べき乗演算子 (`**`) |
There was a problem hiding this comment.
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`となることを利用して書くイディオムが一部では使われています。 |
There was a problem hiding this comment.
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演算子のような意図しない挙動を避けることができます。 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代わりに代入
There was a problem hiding this comment.
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
も合法なんですね。ようはデフォルト引数を引数に渡すときに評価できる式ならいいのか。
There was a problem hiding this comment.
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使ってどうにかした方がいい気はする)
There was a problem hiding this comment.
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ってドキュメント書くものだと思ってたんですがどうやってチェックするんでしょう?
There was a problem hiding this comment.
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ライブラリを使って手動でやるべきですが)
inとかnewとかdeleteみたいなやつはこのくくりで無理やり書かなくてもいいかなー |
console.log(c); // => 3 | ||
``` | ||
|
||
カンマ演算子を活用したテクニックとして[indirect call][]というものがあります。 |
There was a problem hiding this comment.
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 | |||
``` | |||
|
|||
[演算子の優先順序][]は仕様で定義されていますが、多様な演算子が出てきた場合に見分けるのは難しいです。 | |||
そのため、グループ演算子を使い優先順序を明示することが読みやすいコードへとつながります。 | |||
グループ演算子はもっとも優先度が高い演算子となります。 | |||
そのため、グループ演算子使い優先順序を明示することが読みやすいコードへとつながります。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
必ずしもそうではないはず
There was a problem hiding this comment.
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
()をなしの例を書こうしてたのが残ってました。
(これは具体的な例に左右されそうなきがするので削除します)
ひとまず書けたのでマージします。 |
#32 #101 の解説を追加
演算子は検索しにくいので、このページにまとめるという方針で書いています。
なので、若干リファレンスチックになっていますが、この節が特別という感じです。
全体的にざっくりな解説にしてる。