gpt4 book ai didi

javascript - 从第一个函数继承几乎所有内容

转载 作者:行者123 更新时间:2023-12-03 04:15:55 24 4
gpt4 key购买 nike

您好,我正在尝试从第二个函数中编写干燥代码(而不是重复代码),该第二个函数几乎继承了第一个函数的所有内容。

这将是我想要的示例,但它并不干燥:

function firstFunction(){

this.arrayObjectsToElastic = ["hello1", "hello2"]
this.anothervariable1= "anothervariable1"
this.anothervariable2= "anothervariable2"
this.targetVariableToRemove = "something"
return [this.arrayObjectsToElastic]
}


function secondFunction(){

this.arrayObjectsToElastic = ["hello1", "hello2"]
this.anothervariable1= "anothervariable1"
this.anothervariable2= "anothervariable2"

return [this.arrayObjectsToElastic]
}

因此,我不想在第二个函数中从第一个函数“继承”targetVariableToRemove,因为如果是这样,它会在我正在运行的其他一些进程中崩溃。

最佳答案

也许你可以这样做:

function secondFunction(){
return firstFunction().concat(["newContent"]);
}

关于javascript - 从第一个函数继承几乎所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44134956/

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