views:

59

answers:

3

I have something like this:

var test = {};

function blah() {
   test[2] = 'filled';
}

blah(); // ! Hopefully confusion is now averted..

console.log(test);
//result  test -> 2:"filled"

console.log(test[2]);
//result  undefined

I don't understand why I'm getting 'undefined' in the second instance when according to the first instance, the property of that object clearly exists!

Does anyone have any ideas?

Thanks

OK, it seems that folk are getting confused as to what context the code exists in, for clarity sake I have now added the call to the blah(). but please refer to the comment under Jeff B's response!

Here is an example of relevant code so to say:

mydb = ..... //gets created here with relevant credentials
var test = {};
mydb.transaction(
    function(transaction) {
     transaction.executeSql("select * from mytable;", [], function(transaction,result) { 
     var r = result.rows.item(0);
     test[2] = r.title;
}, errorHandler);
    });

    console.log(test);
    //result  test -> 2:"the title"

    console.log(test[2]);
    //result  undefined

@Dancrumb Your mention of the single-threadedness of Javascript gave me an idea, and I tried this:

window.setTimeout(function(){ alert(test[2]); },2000);

and it worked! I got the expected value to alert. Can you suggest how I can get around this without using a 'hack' like that above?

+2  A: 

Because you aren't calling blah()?

Also, you want:

var test = [];

or:

var test = new Array();
Jeff B
Actually blah() does get called. I should have mentioned that this is in connection with HTML 5 client side databases, and the function that blah() represents gets called automatically! So lets assume for clarity sake that 'blah()' gets called. If so, what would cause the unexpected response?
Newbie
The assignment to test is fine. While it would seem unorthodox to use an object, there's nothing inherently incorrect in what the OP is doing with regards to having an object with numeric member names.
Dancrumb
Hmm, when I test it adding `blah()`, using `alert()` instead of `log()`, I get `[object Object]` and `filled`, respectively.
Jeff B
@Dancrumb: I guess I assumed he wanted an array, considering how he was accessing it. But, yes, there is nothing technically wrong with assigning an object.
Jeff B
+2  A: 

EDIT

I ran the following code:

mydb = openDatabase('note','','Example',1024);
var test = {};
mydb.transaction(
    function(transaction) {
     transaction.executeSql("select * from mytable;", [], function(transaction,result) { 
     var r = result.rows.item(0);
     test[2] = r.title;
}, errorHandler);
    });

    console.log(test);

    console.log(test[2]);

in Safari 4.0.5

I got the following:

Object
  No Properties

undefined

This is what I would expect to see. The object test does not have any properties assigned to it until the callback from mydb.transaction occurs and, since Javascript is single threaded, this cannot happen before the calls to console.log.

Since you're getting a different outcome, can you outline what browser and what version you are using?

Dancrumb
Done, please see above! Thanks
Newbie
+1, looks like an asynchronous issue at first glance...
Jonathon
Hi, I'm using Google Chrome, Version : 4.1.249.1045 (42898)
Newbie
A: 

This is pretty clearly an asynchronous issue. The simplest way of getting code to run after you set test[2], is to either put the code right there, or use another callback, and call it after you set test[2].

Russell Leggett
@Russell Leggett : I took your first solution, which I probably should have just done in the beginning! I used the exact same construct somewhere else in the code and it worked fine without any issues, guess the code must be doing alot more processing in the problematic section, hence the asynchronous issues!
Newbie
Thanks alot to everyone who responded on this issue, much appreciated! @Dancrumb : I'd still be interested to hear if you have any ideas other than those offered by Russell Leggett.
Newbie