5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScript で Ruby の ActiveSupport の Object#try のような書き方をするには

Posted at

babel-plugin の @babel/plugin-proposal-optional-chaining を導入すれば、safe navigation 演算子が使えるようになります。「safe navigation 演算子」とは、たとえば Ruby の &. です(Rubyでは「ぼっち演算子」とも呼ばれています)。あるいは ActiveSupport の Object#try です。hoge & hoge.fugahoge?.fuga と記述できるようになります。この文法は 2018/8/21 の時点で The TC39 Process の Stage 1 Proposal です。

導入環境の babel 関連パッケージのバージョンは以下です。scoped module の方を使っています。

    "@babel/core": "^7.0.0-beta.56",
    "@babel/runtime": "^7.0.0-beta.56",
    "babel-core": "^7.0.0-bridge.0",

インストール

npm install --save-dev @babel/plugin-proposal-optional-chaining

設定ファイルの変更は以下です。

▼ .babelrc

  "plugins": [
    [
      "@babel/plugin-proposal-optional-chaining"
    ]

▼ .flowconfig

[options]
esproposal.optional_chaining=enable

リファレンス

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?