当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Should I implement method chaining?

问题描述:

I am writing a javascript DSL and want to know whether method chaining is common practice when implementing javascripts that allow several methods to be called on a single object. I come from a Ruby background where the use of instance_eval is more acceptable for implementing DSLs, so I would prefer to implement my package in a similar way, but I don't want to go against convention.

My options are, through 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 throw this out to the JS gurus to see what is the preferred way to implement this. I suppose this really is a matter of personal preference but there may be a more accepted methodology. Any thought?

分享给朋友:
您可能感兴趣的文章:
随机阅读: