实现非重复代码



在我正在开发的avatargenerator的代码中,我有两个按钮事件处理程序方法,可以使字符鼻子变小或变大。它们几乎包含相同的代码,我想修复(即干燥)

进一步,您可以看到我如何从事件处理程序方法中删除逻辑到一个新函数中,但我不知道如何更改该函数内部的实际逻辑,以便使鼻子变小或变大。所以我基本上想要的是能够改变-到+或其他方式取决于它是从事件处理程序方法调用的函数,使鼻子更小或更大。

有什么建议吗?

现在的代码:

smallerNoseBtn.on('click', function() {
    var nose = AvGen.noses.nose1;
    if (nose.size >= 0.3) {
        nose.x += 3, nose.y += 3;
        nose.size = nose.size -= 0.1;
        nose.size = Math.round(nose.size * 10) / 10;
        nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
    }
});
biggerNoseBtn.on('click', function() {
    var nose = AvGen.noses.nose1;
    if (nose.size <= 1.8) {
        nose.x -= 3, nose.y -= 3;
        nose.size = nose.size += 0.1;
        nose.size = Math.round(nose.size * 10) / 10;
        nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
    }

删除DRY:

smallerNoseBtn.on('click', function() {
    var nose = AvGen.noses.nose1;
    if (nose.size >= 0.3) {
        changeNoseSize(nose, 'smaller');
    }
});
biggerNoseBtn.on('click', function() {
    var nose = AvGen.noses.nose1;
    if (nose.size <= 1.8) {
        changeNoseSize(nose, 'bigger');
    }
});

// The question is what to write in the functions to make it possible to use the variable
// changeSize (which will contain of either 'bigger' or 'smaller')?  
function changeNoseSize(nose, changeSize) {
    nose.x -= 3, nose.y -= 3;
    nose.size = nose.size += 0.1;
    nose.size = Math.round(nose.size * 10) / 10;
    nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
}

您可以传递系数而不是changeSize,然后实现如下内容:

function changeNoseSize(nose, k) {
    nose.x -= k * 3, nose.y -= k * 3;
    nose.size += -k * 0.1;
    nose.size = Math.round(nose.size * 10) / 10;
    nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
}

那么你可以传递1和-1作为k值

你可以这样做:

smallerNoseBtn.on('click', function() {
    if (nose.size >= 0.3) {
        changeNoseSize([-3, -0.1]);
    }
});
biggerNoseBtn.on('click', function() {
    if (nose.size <= 1.8) {
        changeNoseSize([3, 0.1]);
    }
});
function changeNoseSize(changeSize) {
    var nose = AvGen.noses.nose1;
    nose.x -= changeSize[0], nose.y -= changeSize[0];
    nose.size = nose.size += changeSize[1];
    nose.size = Math.round(nose.size * 10) / 10;
    nose.noseObj.transform('S' + nose.size + ', ' + nose.size + ', 0, 0, T' + nose.x + ', ' + nose.y);
}

一个简单的if检查[三元运算符],设置一个变量是应该增加还是减少。

var direction= changeSize==="bigger" ? 1 : -1;
var xyChange = 3 * direction;
nose.x += xyChange; 
nose.y += xyChange;
nose.size += 0.1 * direction;

我个人会传递方向[-1/1]而不是字符串。或者在元素上使用data属性

一个简单的if语句就可以做到。

function changeNoseSize(nose, changeSize) {
    if (changeSize == "bigger") {
        // Bigger Code
    } else if (changeSize == "smaller") {
        // Smaller Code
    } else {
        console.error('no nose size defined')
    }
    // Always run code
}

相关内容

  • 没有找到相关文章

最新更新