Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Strange behaviour in object var member assignment in single statement

New Here ,
Nov 22, 2014 Nov 22, 2014

Hi,

I found this strange behaviour.

In Flash Professional For instance we have:

var dt:Date;

(dt=new Date(2014,0,1,12,00,00,0)).time+=(10*60000);
trace(dt) 

We will note that dt was not changed. I expect that dt is increased of 10 minutes, but it not so.

I repeat a similar code under CSharp and it works like expected.

Works fine otherwise the code:

var dt:Date;
dt=new Date(2014,0,1,12,00,00,0);
dt.time+=(10*60000)

trace(dt)       We will see the time increased of 10 minutes

Regards,

Giorgio

TOPICS
ActionScript
300
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 22, 2014 Nov 22, 2014

I don't know that I would call that strange behaviour moreso that I would call the way you wrote the code strange.  It might be a case where you escaped an error, but I do not see where you changed any property of dt the way you wrote it, so it is tracing the value of dt.  The second version you show is a valid way to write it and it is readily apparent that you are adjusting the time property of dt.

I think you are just processing a new date value when you write it the way you did instead of assigning the value to dt.

Run the following and see what happens...

var dt:Date;
var dd:Date

dd = new Date((dt=new Date(2014,0,1,12,00,00,0)).time+=(10*60000))
trace((dt=new Date(2014,0,1,12,00,00,0)).time+=(10*60000));
trace(dt)
trace(dd)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 22, 2014 Nov 22, 2014

Hi Ned, thank you for reply.

This is a question on which one air application had wrong behavior.

your statement  dd = new Date((dt=new Date(2014,0,1,12,00,00,0)).time+=(10*60000))  resolves but appears onerous.

Let's see the other statements and their behaviours, maybe there is a problem into flash compiler?


var dt:Date;

//Notice, Ticks of 2014-01-1T12:00:00      are     1388574000000
((dt=new Date(2014,0,1,12,00,00,0)).time=(1388574000000+(10*60000)));
trace(dt);//Expected value 2014-01-01T12:10:00 and it is correct

((dt=new Date(2014,0,1,12,00,00,0)).time=dt.time+(10*60000));
trace(dt);//Expected value 2014-01-01T12:10:00 and it is correct


((dt=new Date(2014,0,1,12,00,00,0)).time+=(10*60000));
trace(dt);//Unexpected value

//This statement demostrates that setMinutes works on dt and not on another Date instance.
(dt=new Date(2014,0,1,12,00,00,0)).setMinutes(30,30,30);
trace(dt);//I expect 2014-01-01T12:30:30 and it appears correct.


//Same curious behaviour here.
//I think it is based on the same mechanism of Date:
var o:Object;

(o={uiInner:100}).uiInner+=100;
trace(o.uiInner);//Unexpected value, I expect 200

(o={uiInner:100}).uiInner=o.uiInner+100;
trace(o.uiInner);//Expected value, I expect 200 but I see 100

/*
the mechanism of resolution of:
(o={uiInner:100}).uiInner+=100;

the resolution of first parenthesis couple (o={uiInner:100}) results the new Object 'o'
on which member 'uiInner' we apply addition but is not so (see trace)

With a work around we add 100 to the uiInner member and the trace will show 200:
(o={uiInner:100}).uiInner=o.uiInner+100;
trace(o.uiInner)//Expected value, 200

It appears that (o={uiInner:100}).uiInner is initialized to ZERO when parenteses are resolved.

I checked in others compilers and I have not the earlier unexpected behaviours.
Other compilers are C#, VC++ and I am sure also Java Builder.


*/

Regards and thanks

Giorgio

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2014 Nov 22, 2014

i'm not sure what the point is, but some actionscript classes are chainable and some are not.  you discovered the date class is not and that the compiler throws no error when you try and chain an unchainable class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 22, 2014 Nov 22, 2014

Hi, the point is:

var o:Object;

(o={m:100}).m+=100;

the  o.m  is not valued with 200.

work around:

(o={m:100}).m=o.m+100;

Then, yes o.m. == 200

this can appears trivial but if used in some case, also a bit complex can produce unexpected behavior.

Have you a list of unchainable classes url?

Yes I agree with you, there is something to investigate maybe.

Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 23, 2014 Nov 23, 2014

nope, i have no list.

actually,  they may all be chainable in that you can assign/change a property in the same line that you create the instance, and non-chainable meaning you cannot increment/decrement a property:

var d:Date;

(d=new Date(whatever)).time=whatever else

trace(d.time==whatever else);  // true

var mc:MovieClip;

(mc=new MovieClip()).x=123;

trace(mc.x==123);  // true

var o:Object;

(o={m:100}).m=200;

trace(o.m==200);  // true

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 23, 2014 Nov 23, 2014
LATEST

Yes you are right.

However the 'problem' appears occur only for += operator; operator '=' works fine.

There is to investigate also another cases.

Bye,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines