Efficient Method in JavaScript
<p>Hi How can use a method several times without repeating and making another method in JavaScript.Cause what I did is that I made a method with the same fields and code but only difference is the id. I need one parameter for each method which would be the id.</p>

<pre><code>function enemyGet() {

    new enemyMove2()
    new enemyMove()
    new enemyMove3()
}

  function enemyMove2() {

    var enemyimage = document.getElementById("enemy");
        var leftenemy = parseInt(enemyimage.style.left);
        var topenemy = parseInt(enemyimage.style.top);
        var time = setTimeout("enemyMove2()", 20)

        if (leftenemy &lt; leftplayer) {
            leftenemy = leftenemy + 5;

        }
        if (leftenemy &gt; leftplayer) {
            leftenemy = leftenemy - 5;

        }

        if (topenemy &lt; topplayer) {
            topenemy = topenemy + 5;
        }
        if (topenemy &gt; topplayer) {
            topenemy = topenemy - 5;
        }

        enemyimage.style.left = leftenemy + "px";
        enemyimage.style.top = topenemy + "px";

        var hit_list = $("#x").collision(".lollol");
        hit_list.remove();

}

function enemyMove() {


    var enemyimage = document.getElementById("enemy2");
    var leftenemy = parseInt(enemyimage.style.left);
    var topenemy = parseInt(enemyimage.style.top);
    var time = setTimeout("enemyMove()", 20)

    if (leftenemy &lt; leftplayer) {
        leftenemy = leftenemy + 5;

    }
    if (leftenemy &gt; leftplayer) {
        leftenemy = leftenemy - 5;

    }

    if (topenemy &lt; topplayer) {
        topenemy = topenemy + 5;
    }
    if (topenemy &gt; topplayer) {
        topenemy = topenemy - 5;
    }

    enemyimage.style.left = leftenemy + "px";
    enemyimage.style.top = topenemy + "px";

    var hit_list = $("#x").collision(".lollol");
    hit_list.remove();

}

function enemyMove3() {


    var enemyimage = document.getElementById("enemy3");
    var leftenemy = parseInt(enemyimage.style.left);
    var topenemy = parseInt(enemyimage.style.top);
    var time = setTimeout("enemyMove3()", 20)

    if (leftenemy &lt; leftplayer) {
        leftenemy = leftenemy + 5;

    }
    if (leftenemy &gt; leftplayer) {
        leftenemy = leftenemy - 5;

    }

    if (topenemy &lt; topplayer) {
        topenemy = topenemy + 5;
    }
    if (topenemy &gt; topplayer) {
        topenemy = topenemy - 5;
    }

    enemyimage.style.left = leftenemy + "px";
    enemyimage.style.top = topenemy + "px";

    var hit_list = $("#x").collision(".lollol");
    hit_list.remove();

}
</code></pre>
