我希望根据长度x
重复并堆叠到链上的方法;
const thing = ObjWithMethods
thing
.something()
.somethingElse()
.repeat(() => {
for (let i = 0; i < x; i++) {
thing.repeatedMethod()
}
})
.endMethods();
有没有办法做到这一点?
编辑: 这是我目前的具体示例
import HummusRecipe from 'hummus-recipe';
const getPageCount = require('docx-pdf-pagecount');
const generateFooterText = (index, length) => `Page ${index} / ${length}`;
const applyPdfFooter = (input, output, cb) => {
const throwErr = (err) => {cb(err);};
const recipe = new HummusRecipe(input, output, pdfOptions);
getPageCount(input)
.then((pages) => {
console.log('Pages: ', pages);
recipe
.and(function(recipe) {
for (let i = 0; i < pages; i++) {
const text = generateFooterText(i, pages);
recipe
.editPage(i)
.text(text, 0, 0)
.endPage();
}
})
.endPDF();
cb();
})
.catch(throwErr);
};
export default applyPdfFooter;
答案 0 :(得分:0)
您的配方可以实施为:
class Recipe {
constructor(input, output, options) {
this.page = 0;
}
and(fn) {
fn(this);
return this; // chaining
}
editPage(i) {
this.page = i;
return this;
}
text(...args) {
//...
return this;
}
endPage() {
this.page++;
return this;
}
endPdf() { }
}
...但是我不确定and(...)
方法是否有任何好处...