PDA

View Full Version : Function Vs Classes and positioning


MisterMister
01-19-2005, 07:59 PM
OK, I'm pretty new to actionscript, but this problem has me perplexed.

Here's some sample code to illustrate my question.

main();

function main() {
var test = new SpiffyObject();
trace(test.test);
}

SpiffyObject = function (){
this.test = 1234;
}

It's traces undefined because it doesn't instatiate the object "test". But if I move the constructor for SpiffyObject up above the main() call then it works fine. So I'm guessing that Actionscript lets you call a function with code below, but not instatiate an object with code below.

I've just never encountered that before. Am I doing something wrong, or is that just the way Actionscript does it's thing. I know I can use an #include something.as, and I've been doing that, but this is driving my crazy, I just want to make sure I have this right in my head. I can't find it in any of my Actionscript books.

So to sumise, do classes have to be defined above a new object call? Unlike function calls?

Thanks,
Mark

senocular
01-19-2005, 08:25 PM
"function" first, actionscript will define a function prior to "running" the script. "function" second (or after an = sign) its interpreted as an assignment and will follow execution based on normal script execution, i.e. top to bottom when the full script is actually "run".

MisterMister
01-20-2005, 04:28 AM
Thanks, I had no idea there was a difference. That clears up alot for me.

Thanks again,
Mark