gpt4 book ai didi

javascript - 当两个函数具有某些相似性时进行重构

转载 作者:行者123 更新时间:2023-11-28 03:11:25 25 4
gpt4 key购买 nike

我的应用程序中有两个选项卡,一个是球员选项卡,另一个是教练选项卡。我在“球员”选项卡中有一个功能 1,在“教练”选项卡中有一个功能 2。

函数1

var beforeList = $('#players').val()
$('#players').change(function () {
var afterList = $(this).val()
var selectedPlayer = ''

if (!beforeList) {
selectedPlayer = afterList[0]
$('parent option[value=' + selectedPlayer + ']').add()
$('#injuredPlayer option[value=' + selectedPlayer + ']').add()
} else if (!afterList) {
selectedPlayer = beforeList[0]
$('parent option[value=' + selectedPlayer + ']').remove()
$('#injuredPlayer option[value=' + selectedPlayer + ']').remove()
} else if (beforeList.length > afterList.length) {
selectedPlayer = getselectedPlayer(beforeList, afterList)
$('parent option[value=' + selectedPlayer + ']').remove()
$('#injuredPlayer option[value=' + selectedPlayer + ']').remove()
} else if (beforeList.length < afterList.length) {
selectedPlayer = getselectedPlayer(afterList, beforeList)
$('parent option[value=' + selectedPlayer + ']').add()
$('#injuredPlayer option[value=' + selectedPlayer + ']').add()
}

if (afterList) {
for (var i = 0; i < afterList.length; i++) {
var optionInParentB = ($('#dad option[value=' + afterList[i] + ']').length > 0)
var optionInParentA = ($('#mom option[value=' + afterList[i] + ']').length > 0)
var optionInInjuredPlayer = ($('#injuredPlayer option[value=' + afterList[i] + ']').length > 0)
if (!optionInParentB) {
$('<option/>', {value: afterList[i], html: afterList[i]}).appendTo('#dad')
}
if (!optionInParentA) {
$('<option/>', {value: afterList[i], html: afterList[i]}).appendTo('#mom')
}
if (!optionInInjuredPlayer){
$('<option/>', {value: afterList[i], html: afterList[i]}).appendTo('#injuredPlayer')
}
}
} else {
$('#mom').empty()
$('#dad').empty()
$('#injuredPlayer').empty()
}

beforeList = afterList
})

函数2

var beforeList = $('#coach').val()
$('#coach').change(function () {
var afterList = $(this).val()
var selectedCoach = ''

if (!beforeList) {
selectedCoach = afterList[0]
$('#injuredCoach option[value=' + selectedCoach + ']').add()
} else if (!afterList) {
selectedCoach = beforeList[0]
$('#injuredCoach option[value=' + selectedCoach + ']').remove()
} else if (beforeList.length > afterList.length) {
selectedCoach = getselectedCoach(beforeList, afterList)
$('#injuredCoach option[value=' + selectedCoach + ']').remove()
} else if (beforeList.length < afterList.length) {
selectedCoach = getselectedCoach(afterList, beforeList)
$('#injuredCoach option[value=' + selectedCoach + ']').add()
}

if (afterList) {
for (var i = 0; i < afterList.length; i++) {
var optionInInjuredCoach = ($('#injuredCoach option[value=' + afterList[i] + ']').length > 0)
if (!optionInInjuredCoach){
$('<option/>', {value: afterList[i], html: afterList[i]}).appendTo('#injuredCoach')
}
}
} else {
$('#injuredCoach').empty()
}

beforeList = afterList
})

当我查看这两个功能时,我发现它们非常相似,唯一的区别是球员选项卡有 parent ,而教练选项卡没有。我想知道这些功能是否正常,或者是否应该重构。如果我让它们保持原样,这是不好的做法吗?如果我要重构,我不确定如何使函数足够通用以适应两个选项卡的差异。我很喜欢想法,因为我是 JS 新手,如果我说错了,请原谅我。

最佳答案

如果找不到元素(因为文档中不存在该元素或因为它在函数运行时隐藏),jQuery 往往会默默地失败。如果这对您来说没问题并且没有出现错误,那么一切都很好。

否则,将一些重复的代码移出到新的命名函数中并向其传递一个参数,然后使用它来确定是否对这些元素执行操作。像这样的事情:

// define the function
function doCommonStuff(doDad) {
if (doDad) {
// do Dad stuff
}
}

// call the function
doCommonStuff(true);

关于javascript - 当两个函数具有某些相似性时进行重构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60082844/

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