Why won't this object, loop won't write ?

function lw (bat,bat2,bat3) {
this.bat = bat;
this.bat2 = bat2;
this.bat3 = bat3;
}
var what = new lw ("one","two","three");
for (var apple in what) {
write(what[apple]+"<br/>");
}

Don't know the language you are using. I do know C# and unless that is psudo-code, it won't even compile in C#. In C#, "this" is a keyword to point to the base object's values, something like "private string bat, bat2, bat3;" In that case the routine could look like "void lw (string bat, string bat2, string bat3)" and the {} could be an exact copy, but in no way would you get an array. I have to admit I don't remember if the compiler wouldn't allow you to assign the function to a value, but I believe not.
Here is a C# example:

        /// <summary>
        /// Returns an array of strings
        /// </summary>
        /// <param name="bat">Index 0</param>
        /// <param name="bat2">Index 1</param>
        /// <param name="bat3">Index 2</param>
        /// <returns></returns>
        private static string[] lw(string bat, string bat2, string bat3)
        {
            string[] nw = new string[3];
            nw[0] = bat;
            nw[1] = bat2;
            nw[2] = bat3;
            return nw;
        }
        static void Main(string[] args)
        {
            var Apple = lw("one", "two", "three");
            foreach (var xX in Apple) Console.WriteLine(xX + "<br/>");

...
This produces:
one<br/>
two<br/>
three<br/>

Note that this created an array, populated and returned it to the var Apple. You have to define your Enumerable object and populate it.

It worked for me this way:

var lw = function(){};
lw.prototype = {
    getData : function(bat,bat2,bat3){
        var params = {};
        params[bat] = bat;
        params[bat2] = bat2;
        params[bat3] = bat3;
        return params;
    }

}
var what = new lw();
var result = what.getData("one","two","three");
for (var apple in result) {
    document.write(result[apple]+"<br/>");
}

PS In C#, you can't define "var[]", but you can:

                    private static object[] lw(object bat, object bat2, object bat3)
                    {
                        object[] nw = new object[3];
                        nw[0] = bat;
                        nw[1] = bat2;
                        nw[2] = bat3;
                        return nw;
                    }
                    static void Main(string[] args)
                    {
                        int two = 2;
                        double v = 3.0034;
                        var Apple = lw("one", two, v);
                        foreach (var xX in Apple) Console.Write(xX + "<br/>");

Produces:
one<br/>2<br/>3.0034<br/>

Don't know the language you are using.

@kplcjl: Since this is posted in the Javascript forum, you may assume it's that ;)

I've got to learn to read the article headers!!!
OK, how do you define an array object in JavaScript?
Note that passing a string array for the second object, the output in C# is: one<br/>2<br/>System.String[]<br/>
Also, all languages have some similarities, of course it's a little more difficult to see them in assembler.

Whoa ! This thread went all over the place. I'd like to know why my code isn't working, before I jump in-depth, ebur :)

I am not sure if it is recomended using this, but it is working only if adding 'document.' before 'write':

function lw(bat,bat2,bat3){
    this.bat = bat;
    this.bat2 = bat2;
    this.bat3 = bat3;
}

var what = new lw("one","two","three");
for (var apple in what){
    document.write(what[apple]+"<br/>");
}

Syntax Error was the cause.

eburlea - I never knew there was a prototype method, atlest I hope you call it a method ? :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.