Method Cascades

In the previous section, you found that Dart’s lack of built-in jQuery-like wrapped sets forces you to add and remove classes in two separate steps. Although Dart’s Element class does not support wrapped sets, Dart’s object system does support something very similar—method cascades.

Method cascades let us call several methods on the same objects. A method cascade is invoked with two dots (..) instead of the one dot that calls a method. Here’s the cascaded version of the code that added and removed classes.

 
document.
 
query(​'blockquote'​).
 
classes
 
..remove(​'subdued'​)
 
..add(​'highlighted'​);

That is much more compact and, surprisingly, more readable. It is obvious at a glance that we are performing two operations on ...

Get Dart 1 for Everyone now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.