Copy link to clipboard
Copied
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!!!
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),
Copy link to clipboard
Copied
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 { }.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
thanks for replying! no remains of any clear, but very good answers!
Copy link to clipboard
Copied
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!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now