任何人都可以帮助我如何在JavaScript(2)中执行以下操作.ADD(3)(4)(5)..



我需要添加诸如2 3 4 5 之类的数字...我被要求使用原型来执行此操作吗?

我想这就是你的意思:

Number.prototype.add = function(number){
    return this.valueOf() + number
} 

用法:

(2).add(3) //5

(2).add(3).add(4) //9


如果您想要咖喱:

Number.prototype.curry = function(end){
    if (end <= this.valueOf()) {
        throw "End must be larger."
    } else {
        return (this.valueOf() + end) * (1 - this.valueOf() + end / 2
    }
} 

用法:

(2).curry(5) //equivalent to 2 + 3 + 4 + 5 = 14

最新更新