Javascript styling methods

I have an object that I created using this snip-it that looks like this:

  ...
  var steps = new Array();
  this.createStep = function(){steps.push(new step()); return steps[steps.length-1];}; 
  this.getSteps = function(){return steps;}; //returns Array
  this.removeStep = function(pos){steps.splice(parseInt(pos), 1);}; // integer possition zero base
  this.insertStep = function(pos){steps.splice(parseInt(pos),0, new step());};

And it works great:

...
var newStep = wfObj.createStep();
newStep.setTitle('Step-'+i);
newStep.setStatus('New');

But it is not

var newStep = wfObj.createStep().setTitle('Step-'+i).setStatus('New');

Can someone please tell me how to fix this or even name when you link such methods?

+5
source share
2 answers

As Ned said , it is sometimes called a free interface . It is also sometimes called a chain of methods , as you heard.

You probably have code somewhere that looks like this:

this.setTitle = function(newTitle) {
    title = newTitle;
};

Change this to:

this.setTitle = function(newTitle) {
    title = newTitle;
    return this;
};

Do the same for setStatus.

+4
source

. , this.

+6

All Articles