Uma opção poderia ser criar uma "classe" (ou o equivalente do JavaScript neste caso), que se comporta como um functionoid / functor, e armazena currentSeconds
, então chama para essa 'classe' [sic] de um proxy função para garantir que você ainda esteja chamando apenas onUpdate
do código de chamada:
var Alerter = (function () {
function Alerter(currentSeconds) {
this.currentSeconds = currentSeconds;
}
Alerter.prototype.onUpdate = function () {
this.updateRoute();
this.updateAlerts();
};
Alerter.prototype.updateAlerts = function () {
for (var i = 0; i < alertMarkers.length; ++i)
this.oneAlertUpdate(alertMarkers[i], alertAll[i]);
};
Alerter.prototype.oneAlertUpdate = function (alertMarker, alert) {
this.updateAlertMarker(alertMarker, alert[entryIndex]);
this.changeMarkerOpacityIfNeeded(alertMarker);
};
Alerter.prototype.changeMarkerOpacityIfNeeded = function (alertMarker) {
//set opacity based on currentSeconds
var opacity = alertMarker.opacityTime < this.currentSeconds ? 1 : 0.5;
if (alertMarker.getOpacity() != opacity)
alertMarker.setOpacity(opacity);
};
return Alerter;
}());
function onUpdate(currentSeconds) {
new Alerter(currentSeconds).onUpdate();
}
A principal desvantagem disso é a complexidade adicional no código, que apenas nega a maior parte do benefício em evitar passar repetidamente a mesma variável em primeiro lugar.
No entanto, evita esse bit de duplicação, e o código de chamada não deve notar nenhuma diferença, pelo menos:
var currentSeconds = 5;
onUpdate(currentSeconds);
Mas, no geral, não tenho certeza se essa abordagem é realmente "mais limpa"; as soluções mais simples são geralmente as melhores.