小编Chr*_*ann的帖子

是否可以使用另一个原型而不是Function.prototype创建一个函数?

我正在使用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)

javascript prototype function

5
推荐指数
1
解决办法
352
查看次数

标签 统计

function ×1

javascript ×1

prototype ×1