Should I implement a chain of methods?

I am writing javascript DSL and want to know if the method chain is common practice when implementing javascripts that allow you to call multiple methods on the same object. I came from the Ruby background, where using instance_eval is more acceptable for implementing DSL, so I would prefer to implement my package in a similar way, but I don't want to go against the convention.

My options, for example:

MyObject('test').method1(function() { console.log('hi'); }).method2(function() { console.log('bye'); }); 

or

 MyObject('test', function() { this.method1(function() { console.log('hi'); }); this.method2(function() { console.log('bye'); }); }); 

I thought I would drop this JS guru to find out what is the preferred way to implement this. I believe this is indeed a matter of personal preference, but there may be a more acceptable methodology. Any thought?

+6
source share

Source: https://habr.com/ru/post/972856/


All Articles