Skip to content

Latest commit

 

History

History
55 lines (30 loc) · 826 Bytes

fluent.interface.md

File metadata and controls

55 lines (30 loc) · 826 Bytes

Fluent Interface 📕

importance

Makes your code much easier to read and to write.

without Fluent api

 // with variables                        

  const summed = calc.add(1, 2); 
  const squared = calc.square(summed);
  calc.display(squared);
      
 // without variables 

  calc.display(calc.square(calc.add(1, 2)));

with Fluent api

  calc
      .add(1, 2)
      .square()
      .display();

Real world example

  var $ = new jQuery();
      $("input")
      .prop("disabled", false);
      .val("someValue");
  // jQuery has its fluent API to thank for its simplicity

core principles

Any fluent methods must return the parent object. Fluent apis mutate the object itself.