article

function/method chaining

[WHAT]

  1. ] a common practice//technique in javascript development , that can be used to simplify code in scenarios that involve calling multiple functions on the same object consecutively

[WHY]

  1. ] can tidy up code quite a bit, write a single line of code versus multiple lines of code,
  2. ] avoid duplication, DRY

[WHERE]

  1. ] wherever you would call multiple methods on the same object

[WHEN]

  1. ]

[EXAMPLE]

  1. ] myCar.start().windowsDown().musicOn().shift('D').accelerate(10)

[HOW-TO]

  1. ] CREATE
    1. ] var Car = function() {
        this.type = 'sports';
        this.color = 'brown';
        this.started = false;
      };
    2. Car.prototype.start = function() {
        this.started = true;
    3.   console.log = 'the car has started:'+this.started ;
      };
  2. ] USE
    1. ] use the .dot operator to chain multiple functions together, see example above

[REFERENCE]

  1. ] http://schier.co/blog/2013/11/14/method-chaining-in-javascript.html 
  2. ] also known as the Fluent Interface pattern.

 

Details Photos Edit more

Details

ID: 4624

NAME: 010-function-chaining

DESCRIPTION: by Don Sagrott, founder @sospep.com - a look at how you can use function chaining to execute a series of methods on your javascript objects

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: 0

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and