LoginSignup
28
18

More than 5 years have passed since last update.

Dartで良さそうな演算子

Last updated at Posted at 2019-01-04

本文について

めちゃくちゃ主観的に実装で使いたいなと思った演算子をピックアップしました。
自分の頭の整理と、どなたかの参考になればと思います。

??=

  • 左辺がnullであるとき、右辺を代入します。
  • 何かしら値を入れたい時に使いたいです
// 
var value = "a";
// bをnullにします
var b = null
//valueが代入されます。
b ??= value;

is

  • 型判定をするときに使います。
  • 左辺が右辺の型と同じであるとき,trueを返します

var a = "value"
//Stringなのでtrueがかえります
a is String

cascade

  • objectをわざわざ繰り返し書かなくても、property操作を可能にする
  • 注意しなければいけないのは、返り値がわからない場合使えない
  • 注意しなければいけないのは左辺がわからない場合使えない
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));

上記と同じことを行なっている

querySelector('#confirm') //Objectが返り値となる
  ..text = 'Confirm' // Use its members.
  ..classes.add('important')
  ..onClick.listen((e) => window.alert('Confirmed!'));

参考

28
18
2

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
28
18