我正在使用JavaScript中的解析器组合器库.为此,我想创建可以像任何其他函数一样调用的函数,但也有成员函数,可以依次调用它们以根据它们所附加的函数生成输出(例如组合器).
我当然可以将成员添加到这样的函数中:
//the functions I want to add additional members to
function hello(x) {
return "Hello " + x;
}
function goodbye(x) {
return "Goodbye " + x;
}
//The function I want as a member of the above functions.
//it creates another function based on the function it is
//attached to.
function double() {
var that = this;
return function(x) {
return that(x) + ", " + that(x);
};
}
//I can attach them manually to the function objects: …Run Code Online (Sandbox Code Playgroud)