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

What is the difference between a literal value and a constructor?

Guest
May 07, 2014 May 07, 2014

1)what is the difference between a literal value and a constructor?

example:

var someArray:Array = new Array(1,2,3); // Array constructor

var someArray:Array = [1, 2, 3]; // literal value


2)what is the "new" operator, and it serves?

3)what is a constructor?

I'm reading AS3 Fundamentals

, and so far I have failed to understand that.

Learning ActionScript 3 | Adobe Developer Connection


Thnks ALL!!!

TOPICS
ActionScript
748
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

correct answers 1 Correct answer

LEGEND , May 07, 2014 May 07, 2014

Nothing more than shorthand ECMAScript. You can do this in JavaScript as well. Both objects are instances of the Array class.

When you generate a large matrix you will appreciate the shorthand approach. It's much more friendly to read. For example:

var tileMapA:Array = [ [1, 1, 1, 1, 1],

                       [2, 2, 2, 2, 2],

                       [3, 3, 3, 3, 3] ];

VS:

var tileMapB:Array = new Array( new Array(1, 1, 1, 1, 1),

                                new Array(2, 2, 2, 2, 2),

         

...
Translate
LEGEND ,
May 07, 2014 May 07, 2014

Nothing more than shorthand ECMAScript. You can do this in JavaScript as well. Both objects are instances of the Array class.

When you generate a large matrix you will appreciate the shorthand approach. It's much more friendly to read. For example:

var tileMapA:Array = [ [1, 1, 1, 1, 1],

                       [2, 2, 2, 2, 2],

                       [3, 3, 3, 3, 3] ];

VS:

var tileMapB:Array = new Array( new Array(1, 1, 1, 1, 1),

                                new Array(2, 2, 2, 2, 2),

                                new Array(3, 3, 3, 3, 3) );

Not a huge difference immediately but expand these into much more complex nested Array structures and you'll see the difference clearly. Same thing goes for new Object() or just { }.

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
Guide ,
May 07, 2014 May 07, 2014

I think it mainly comes down to speed. However, which version is fastest probably varies by Flash Player version, which you usually can't control.

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 ,
May 07, 2014 May 07, 2014

And to directly address your question on the constructor, in class based AS3 the same constructor is used in both instances.

e.g.

var a:Array = [1];

var b:Array = new Array(1);

trace('a:' + a.constructor + ', b:' + b.constructor + ', same:' + (a.constructor === b.constructor));

Traces:

a:[class Array], b:[class Array], same: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
Guest
May 08, 2014 May 08, 2014

thanks for replying! no remains of any clear, but very good answers!

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 ,
May 09, 2014 May 09, 2014
LATEST

You should feel free to use either form. You'll find this quite a bit in programming. Many ways to do essentially the same thing!

You're welcome and good luck!

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