Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating functions in Javascript

Tags:

javascript

I'm currently working in a js-component and i was wondering if there's a better way to concatenate functions in javascript then returning this. I have a sample code working here, and that's how i solved the problem.

function hi(){
  console.log('hi');
  return this;
}
function bye(){
  console.log('bye');
  return this;
}
function Test(){};

Test.prototype.hi = hi;
Test.prototype.bye = bye;

var x = new Test();

x
 .hi() //hi
 .bye(); //bye
like image 446
Igor Santana Avatar asked Apr 22 '26 18:04

Igor Santana


1 Answers

This is a well known pattern called a fluent interface - it can certainly help write code in certain circumstances, but like everything it has a purpose but shouldn't be used for everything.

like image 107
Jamiec Avatar answered May 05 '26 11:05

Jamiec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!