演算子
演算子の優先順位(低い順位から)
Section titled “演算子の優先順位(低い順位から)”pipeパイプラインcoalNull合体or論理ORand論理ANDborビットORbxorビットXORbandビットANDeq/neq/not eq/instanceof等価 / 型チェックlt/gt/le(lt eq)/ge(gt eq) 比較shl/shr/ushrビットシフトadd/sub加算 / 減算mul/div/mod乗算 / 除算 / 剰余powべき乗- 単項:
not/neg/bnot/typeof/void/await/delete/new/add\(前置++) /sub\(前置--) - 後置:
.アクセス /\.オプショナルチェイニング /[args]呼び出し /[\expr]計算アクセス /asキャスト /\add(後置++) /\sub(後置--) - 基本: リテラル、識別子、括弧
パイプライン
Section titled “パイプライン”data pipe filterdata pipe filter pipe mapdata pipe transform[extra-arg]data pipe .method[arg]コンパイル結果:
filter(data)map(filter(data))transform(data, extra_arg)data.method(arg)const x be 42let y be 10y be 20算術演算と代入を組み合わせた複合代入演算子:
x add be 1 -- x += 1x sub be 1 -- x -= 1x mul be 2 -- x *= 2x div be 2 -- x /= 2x mod be 3 -- x %= 3x pow be 2 -- x **= 2ビット演算の複合代入:
x band be 255 -- x &= 255x bor be 1 -- x |= 1x bxor be mask -- x ^= maskx shl be 2 -- x <<= 2x shr be 1 -- x >>= 1x ushr be 1 -- x >>>= 1論理演算の複合代入:
x and be true -- x &&= truex or be false -- x ||= falsex coal be 0 -- x ??= 0プロパティアクセスやインデックスアクセスにも使用可能:
obj.count add be 1 -- obj.count += 1arr[\i] mul be 2 -- arr[i] *= 2a add b -- a + ba sub b -- a - ba mul b -- a * ba div b -- a / ba mod b -- a % ba pow b -- a ** bneg x -- -xa band b -- a & b (ビットAND)a bor b -- a | b (ビットOR)a bxor b -- a ^ b (ビットXOR)bnot x -- ~x (ビットNOT)a shl b -- a << b (左シフト)a shr b -- a >> b (右シフト)a ushr b -- a >>> b (符号なし右シフト)eq の右辺が型名(string, number, null, 大文字始まりのクラス名など)の場合は型チェックになり、それ以外は厳密等価比較(===)になります。
不等価には neq または not eq を使います。以下はどちらも le と同じ意味です: lt eq。以下はどちらも ge と同じ意味です: gt eq。
a eq b -- a === ba neq b -- a !== ba not eq b -- a !== b(別の書き方)a lt b -- a < ba gt b -- a > ba le b -- a <= ba lt eq b -- a <= b(別の書き方)a ge b -- a >= ba gt eq b -- a >= b(別の書き方)a eq string -- typeof a === "string"(型チェック)..(包含)と ...(排他)を使って数値範囲から配列を生成します:
const a be [0..5] -- [0, 1, 2, 3, 4, 5]const b be [0...5] -- [0, 1, 2, 3, 4]const c be [1..10] -- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]const d be [1...10] -- [1, 2, 3, 4, 5, 6, 7, 8, 9]スライス(切り出し)
Section titled “スライス(切り出し)”\ プレフィックスと ..(包含)または ...(排他)をブラケットアクセス内で使い、配列の一部を切り出します:
const numbers be [0, 1, 2, 3, 4, 5, 6]const middle be numbers[\2..5] -- [2, 3, 4, 5]const partial be numbers[\2...5] -- [2, 3, 4]スプライス(部分置換)
Section titled “スプライス(部分置換)”スライスに代入することで配列の一部を置換できます:
numbers[\2..4] be [///a///; ///b///; ///c///]-- numbers は [0, 1, "a", "b", "c", 5, 6] になります計算プロパティアクセス
Section titled “計算プロパティアクセス”\ を括弧内で使うことで、式による配列オブジェクトアクセスを行います:
const val be arr[\i] -- arr[i]const item be obj[\key] -- obj[key]arr[\0] be ///new/// -- arr[0] = "new"\ プレフィックスにより、関数呼び出し(f[x])とプロパティアクセス(arr[\x])を区別します。
オプショナルチェイニング
Section titled “オプショナルチェイニング”\. でオプショナルチェイニング(JSの ?.)を表現します:
const name be user\.name -- user?.nameconst val be obj\.method[1; 2] -- obj?.method(1, 2)const deep be a\.b\.c -- a?.b?.cコンパイル結果:
const name = user?.name;const val = obj?.method(1, 2);const deep = a?.b?.c;配列から変数に値を取り出します:
const weather be [///Sunny///; ///Rainy///]const [today; tomorrow] be weather
-- 変数の値を入れ替える[today; tomorrow] be [tomorrow; today]オブジェクト分割代入
Section titled “オブジェクト分割代入”object[...] を使ってオブジェクトからプロパティを取り出します:
const config be [host be ///localhost///, port be 8080]const object[host; port] be configコンパイル結果:
const config = { host: "localhost", port: 8080 };const { host, port } = config;a and b -- a && ba or b -- a || bnot x -- !xNull合体(Nullish Coalescing)
Section titled “Null合体(Nullish Coalescing)”coal 演算子は、左辺が null または undefined の場合に右辺の値を返します:
a coal b -- a ?? ba coal b coal c -- a ?? b ?? cコンパイル結果:
a ?? b;a ?? b ?? c;or との違い: or はすべてのfalsy値(false, 0, "", null, undefined)をfalseとして扱いますが、coal は null と undefined のみを「空」として扱います。0 や false や "" を保持したい場合は coal を使ってください。
const port be config.port coal 3000 -- portがnull/undefinedの場合のみ3000を使用const name be user.name coal ///guest/// -- nameがnull/undefinedの場合のみ"guest"を使用eq は型名の前に置くと型チェックとして機能します。
x eq string -- typeof x === "string"x eq null -- x === nullx eq MyClass -- x instanceof MyClassx instanceof Y -- x instanceof Ytypeof x -- typeof xvoid x -- void xインクリメント / デクリメント
Section titled “インクリメント / デクリメント”\ と add / sub を組み合わせて、後置・前置のインクリメント/デクリメントを表現できます:
| Purus | JS | 説明 |
|---|---|---|
x\add | x++ | 後置インクリメント |
x\sub | x-- | 後置デクリメント |
add\x | ++x | 前置インクリメント |
sub\x | --x | 前置デクリメント |
let i be 0i\add -- i++ (0を返し、iは1になる)add\i -- ++i (iは2になり、2を返す)i\sub -- i-- (2を返し、iは1になる)sub\i -- --i (iは0になり、0を返す)コンパイル結果:
let i = 0;i++; // 0を返し、iは1になる++i; // iは2になり、2を返すi--; // 2を返し、iは1になる--i; // iは0になり、0を返す