实现非重复性代码

时间:2013-06-12 13:17:01

标签: javascript jquery event-handling dry

在我正在研究的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);
}

4 个答案:

答案 0 :(得分:1)

一个简单的if check [三元运算符],如果你应该增加或减少,它设置一个变量。

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

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

答案 1 :(得分:1)

您可以传递系数而不是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值。

答案 2 :(得分:1)

你可以这样做:

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);
}

答案 3 :(得分:0)

一个简单的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

}