Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can I pass an array that needs to be looped over as a (constructor) function argument?

I have written a constructor function to create multiple other functions that differ only slightly. The problem is I need to pass each function an array of properties to iterate over.

I have a loop inside the function but when I call the function I obviously can't write the argument using 'i' (like the example below) because 'i' isn't defined yet. How are you supposed to code a situation like this? Please let me know if what I am doing is crazy, i'm new to js.

// loading sequence constructor function
function LoadingSequence(properties) {
    this.properties = properties;
    var loadingSequence = [];
    var sequenceStep = {};

    // for each step in allSteps, add an animation sequence to the loader
    for (var i = 0; i < allSteps.length; i++ ) {
        sequenceStep = {
            e: targetElement,
            properties,
            o: {
                duration: allSteps[i].duration,
                easing: allSteps[i].easing
            }
        }
        loadingSequence.push( sequenceStep );
    }
    return loadingSequence;
}

// props for main sky loading sequence
var mainSkyProps = {
    opacity: [allSteps[i].skyCol.main[0], allSteps[i].skyCol.main[1]],
    rotateZ: [allSteps[i].degRotate[0], allSteps[i].degRotate[1]]
}
// main sky loading sequence
var mainSkyLS = new LoadingSequence(
    mainSkyProperties
);

So you could create a function inside of your Loadingsequence eg function Loadingsequence (properties) {

this.someName (arg, arg){ return something...... }

} http://www.w3schools.com/js/js_object_methods.asp

Hey Ryan, thanks for the reply. I'm not sure what you mean though. How would adding an object method let me pass in an argument that would work in the loop?

Steven Parker
Steven Parker
243,656 Points

I found this old question, do you still need an answer?