Javascript Refractor Problem

Hi,

I would like to do the refractoring for the re-use. However, I have some problems in doing it with the following coding:

    var prismVertexPositionBuffer;
    var prismVertexPositionBuffer2;
    var prismVertexPositionBuffer3;
    var prismVertexNormalBuffer;
    var prismVertexColorBuffer;
    var prismVertexColorBuffer2;
    var prismVertexIndexBuffer;
    var prismVertexIndexBuffer2;
    var prismVertexIndices;
    var prismVertexIndices2;
    var unpackedColors;
    var vectices;
    var normalData;
    var height = 2;
    var n = 12
    var twoPi = 2.0 * 3.14159;

    function initBuffers() {
        vertices = [0, 0, 0];
        vertices2 = [0, height, 0];
        vertices3 = [];
        normalData = [0, 0, 1];
        unpackedColors = [1, 0, 0, 1];
        unpackedColors2 = [];
        prismVertexIndices = [];
        prismVertexIndices2 = [];

        for (var j = 0; j < n; j++) {
            vertices = vertices.concat([Math.cos((j - 1) * twoPi / n), 0.0, Math.sin((j - 1) * twoPi / n)]);
            vertices2 = vertices2.concat([Math.cos((j - 1) * twoPi / n), height, Math.sin((j - 1) * twoPi / n)]);
            vertices3 = vertices3.concat([Math.cos((j - 1) * twoPi / n), 0.0, Math.sin((j - 1) * twoPi / n)]);
            vertices3 = vertices3.concat([Math.cos((j - 1) * twoPi / n), height, Math.sin((j - 1) * twoPi / n)]);
            normalData = normalData.concat([0, 0, 1]);
            unpackedColors = unpackedColors.concat([1.0, 0.0, 0.0, 1.0]);
            unpackedColors2 = unpackedColors2.concat([1.0, 0.5, 0.0, 1.0]);
            unpackedColors2 = unpackedColors2.concat([1.0, 0.5, 0.0, 1.0]);
            prismVertexIndices = prismVertexIndices.concat([0, j + 1, (j + 1 < n ? j + 2 : 1)]);
            prismVertexIndices2 = prismVertexIndices2.concat([j * 2, j * 2 + 1, (j + 1 < n ? j * 2 + 2 : 1)]);
            prismVertexIndices2 = prismVertexIndices2.concat([j * 2 + 1, (j + 1 < n ? j * 2 + 2 : 1), (j + 1 < n ? j * 2 + 3 : 2)]);
        }
}

I want to extract the content of initBuffer() into another js file as a function. However, when I do it, the parameters cannot be returned. Is there any methods to refractoring this code?

Is the above code working code, or is it a part of your attempt to refactor something?

The function relies on external values, those being height, n, and twoPi

If you want the function to return a result set, instead of modifying global variables, that can be done too.

Basically, more information is needed concerning just what you’re wanting to achieve.