[thelist] [javascript] object literal question

m.milicevic me at machak.com
Wed Mar 8 10:14:26 CST 2006


Hi Tom,

you could also use associative arrays e.g. something like:


var my_array = new Array();

my_array["widget1"] = {name:"widget1",price:100,toggle:1};
my_array["widget2"] = {name:"widget2",price:200,toggle:1};

//alert(my_array['widget1'].price);

for (var i in my_array)
{
 alert(my_array[i].price);
}

-m.j.m


> Say I have this object literal:
>
> var PricedProduct = {
>  name:null,
>  price:0,
>  toggle:0,
>
>  add:function(pname, pprice, ptoggle){
>    this.name = pname;
>    this.price = pprice;
>    this.toggle = ptoggle;
>  }
> }
>
> And say I add 3 products, which by name are "widge1", "widge2" and 
> "widge3". This is probably
> painfully obvious to someone, but how to I access the properties of say 
> "widge2"? What tells me
> how many objects I have?
>
> I figured I could make an array add push() the new product name to it each 
> time I run the add()
> method so I have an array with all the names (so I know I have 3 products 
> by testing
> array.length). But even if I have the name, again, how do I grab that one 
> particular object? Or am
> I going about building this object the wrong way?
>
> In the end, when a user prints a page I have to grab this list of products 
> and write an invoice
> out for each product that is toggled 'on' (or 1). I'd need to grab the 
> name and price for each one
> of these and build them into a table, which is why I am going down this 
> path..
>
> Tom
>
> http://www.pixelmech.com/
> http://www.crossandthrone.com/
>
>
> Proponents of higher-order theories of consciousness argue that 
> consciousness is explained by the relation between two levels of mental 
> states in which a higher-order mental state takes another mental state. If 
> you mention this to Chuck Norris, expect an explosive roundhouse kick to 
> the face for spouting too much fancy-talk.
>
> -- 
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt ! 




More information about the thelist mailing list