gpt4 book ai didi

javascript - 减少javascript代码

转载 作者:行者123 更新时间:2023-11-30 09:35:57 25 4
gpt4 key购买 nike

我的 Angular 项目中有两个函数,一个是 aceitaProposta,它执行以下操作:

aceitaProposta() {
this.aceita();
// some other code
}

我有 recusaProposta:

recusaProposta() {
this.recusa();
// some other code
}

现在,this.recusathis.aceita 之间的唯一区别是第一个递增一个属性,第二个递减它。我想知道是否有一种方法可以将这两个函数转换为一个函数,只使用 if 之类的东西来识别它是递减还是递增。这样我就可以在 aceitaPropostarecusaProposta 中调用相同的函数。示例:

aceitaProposta() {
this.incrementDecrement();
// some other code
}

recusaProposta() {
this.incrementDecrement();
// some other code
}

最佳答案

应该很简单。

function incrementDecrement(inc: boolean)
{
inc ? this.prop++ : this.prop--;
}

关于javascript - 减少javascript代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com