Skip to main content
May 7, 2014
Answered

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

  • May 7, 2014
  • 4 replies
  • 823 views

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!!!

This topic has been closed for replies.
Correct answer sinious

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 { }.

4 replies

May 9, 2014

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

sinious
Legend
May 9, 2014

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!

sinious
Legend
May 7, 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

Amy Blankenship
Legend
May 7, 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.

sinious
siniousCorrect answer
Legend
May 7, 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 { }.