Array initialization problem
I have the following code:
var intrebari = new Array();
var i = 0;
intrebar开发者_运维百科i[i]['enunt'] = 'test';
alert(intrebari[i]['enunt']);
The problem is that when I run it it says that intrebari
is undefined. Why?
Yes interbari[0] is null , so it cannot be object - and for adding into array use push instead of indexes
var intrebari = [];
intrebari.push({ 'enunt': 'test' });
alert(intrebari[i]['enunt']);
This will work
var intrebari = new Array();
var i = 0;
intrebari[i] = new Object()
intrebari[i]['enunt'] = 'test';
alert(intrebari[i]['enunt']);
You need to assign something to intrebari[i]
before you can access any properties of it, "by default" its value is undefined
that doesn't have any properties. For example:
intrebari[i] = new Object();
intrebari[i]["enunt"] = "test";
alert(intrebari[i]["enunt"]);
精彩评论