View Full Version : Bug in with-statement?
cons
February 2nd, 2008, 11:10 AM
According to the docs, the scope chain used by the with statement to resolve identifiers is: innermost with-statement -> outermost with-statement -> activation object -> etc. If this would be true, the following code should output "propOfMyObject":
var foo:String = "propOfActivationObject";
var myObject:Object = {foo:"propOfMyObject"};
with (myObject) {
trace(foo); //outputs: "propOfActivationObject"
}But it doesn't. Instead the local variable is shadowing the property in myObject. It seems like the scope chain rather is: activation object -> innermost with-statement -> outermost with-statement -> etc. Which means either the docs or the with statement is falsy. Am I right?
Dazzer
February 2nd, 2008, 11:40 AM
try
trace(.foo);
cons
February 2nd, 2008, 06:25 PM
try
ActionScript Code:
trace(.foo);
That results in a "1083: Syntax error: dot is unexpected.
Dazzer
February 2nd, 2008, 07:04 PM
Hmmm I thought it might be that. Now I guess I'm not sure.
How about
trace(this.foo);
Dazzer
February 2nd, 2008, 08:03 PM
And what happens if you take away the previous variable declaration?
pallzoltan
February 3rd, 2008, 12:32 PM
believe it or not, it outbputs "propOfMyObject" in my flash :)
cons
February 3rd, 2008, 02:09 PM
After some postings in another forum, it seems like this is indeed a bug in AS3:
1) Create a new blank as3-.fla file
2) In the actions-window (that is, the code will reside directly in the main time-line) , write:
var foo:String = "valOfFooInActivationObject";
var myObject:Object = {foo:"valOfFooInMyObject"};
with (myObject) {
trace(foo); //outputs: "valOfFooInMyObject"
}Everything works perfect here exactly as the docs say. The myObject is linked in before the activation object (which contains all local variables etc). So when resolving foo, it first looks in myObject and finds it there. If it would not have found it there (try to change foo to fii in myObject for instance), it would have continued to search in the next object in the scope chain. The next object would have been the activation object where all local variables resides. There it would have found our local variable foo and would have outputed "valOfFooInActivationObject". So far so good.
BUT.
This only works directly on the main time-line. As soon as you write this code within a function of any sort, it seems like myObject is linked in after the activation object:
function test() {
var foo:String = "valOfFooInActivationObject";
var myObject:Object = {foo:"valOfFooInMyObject"};
with (myObject) {
trace(foo); //outputs: "valOfFooInActivationObject"! BUG!
}
}
test();This is definately a bug and exact opposite to what the docs and its example says.
Dazzer
February 3rd, 2008, 05:44 PM
Thanks for letting us know ^^
However, you'll never run into these type of problems if you work with real classes, and encapsulated data.
Does the same bug happen with functions?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.