PDA

View Full Version : 'pointers' in JavaScript??


trsands
01-29-2004, 03:45 AM
I have an array of objects..... objArray

each object consists of:

obj.item
obj.quantity
obj.price

to access and element i refer to it as

objArray[i].price etc...

I want to pass objArray[i] to a funtion then within the function reference the elements

e.g.
function printobj(prtobj)
{
document.write(prtobj.price;
}

....
printobj(objArray[2])
...
or If I try to create a variable
var pobj = objArray[3]

printobj(pobj)

......


for some reason it rejects this

glenngv
01-29-2004, 04:24 AM
This works for me.

<script>
var arr = new Array();

arr[0] = new Object();
arr[0].item = "item1"
arr[0].quantity = 1;
arr[0].price = 100;

arr[1] = new Object();
arr[1].item = "item2"
arr[1].quantity = 2;
arr[1].price = 200;

function printobj(obj){
alert("item:"+obj.item+"\nquantity:"+obj.quantity+"\nprice:"+obj.price);
}
printobj(arr[0]);
printobj(arr[1]);
</script>