global object question

why doesn’t this work:


function init()
return {posters:{home:mainposters}
	    posterlists:{home:posters.home}		
                }
end function

where mainposters is a function.

I get
runtime error ec: ‘Dot’ Operator attempted with invalid BrightScript Component or interface reference.

089: posterlists:{home:posters.home}

if I change it to:

		posters:{home:mainposters}
		posterlists:{home:m.posters}	

the function will return but posterlists.home is ROT:roInvalid

Forgive me if I re-format that so I can see it better:


function init()
    return {
        posters: {
            home: mainposters
        }
	posterlists: {
            home: posters.home
        }
    }
end function

There is no posters variable at the point you are doing this assignment. That happens later. During the assignment, it’s generating an anonymous data structure, and after it’s created, assigning it to a variable (or in this case returning it).

In the case of


function init()
    return {
        posters: {
            home: mainposters
        }
	posterlists: {
            home: m.posters
        }
    }
end function

You are getting invalid because you haven’t assigned anything to m.posters at the point of the assignment, which is when this is evaluated. Similar to above (you may have an m with this data later, but not at this point from this function alone.

What you have here is a bit of a chicken and egg problem. Try this:


function init()
    data =  {
        posters: {
            home: mainposters
        }
    }
    ' Point posterlists to the same AA in data.posters
    data.posterlists = data.posters ' data.posters already exists from above
    return data
end function

cool! Why didn’t I think of that???

Thanks K.

  • Joel