ECMAScript data types: Difference between revisions
m (AS3 Variable Types moved to ECMAScript data types: AS3 is basically ECMAScript) |
m (using an external editor) |
||
Line 12: | Line 12: | ||
Boolean, int, Null, Number, String, uint, and void. | Boolean, int, Null, Number, String, uint, and void. | ||
=== Numbers === | === Numbers === | ||
Line 37: | Line 40: | ||
== Complex data types in ActionScript core classes == | == Complex data types in ActionScript core classes == | ||
The full list include: Object, Array, Date, Error, Function, RegExp, XML, and XMLList | The full list of ActionScript 2 datatypes include: Object, Array, Date, Error, Function, RegExp, XML, and XMLList. | ||
=== Arrays === | === Arrays === | ||
=== | To create an array and to add some values, you can use something like: | ||
var mylist = Array (); | |||
mylist[]=1; | |||
mylist[]=2; | |||
mylist[]=3; | |||
or | |||
var mylist = [1,2,3]; | |||
You can define the lenght of an array: | |||
var mylist = new Array(12); | |||
But it then can be increased later, e.g. this is ok: | |||
mylist(14) = "bla"; | |||
There are several sorts of arrays: | |||
* normal arrays (as seen above) | |||
var xx = [1,2,3] | |||
But then we also can have | |||
* typed arrays (ECMAScript 4 / JavaScript 2 - not tested !) | |||
var xx = [ int] (12) // an array of 13 integer elements | |||
var xx = [1,2,3] : [int] // an array filled with 2 int. elements | |||
var xx = new [int] (1,2,3) // same | |||
* associative arrays (records) | |||
var hash = { x:10, y:"foo" } | |||
:This will return 10: | |||
alert(hash ['x']) | |||
* typed associative arrays (ECMAScript 4 / JavaScript 2 - not tested !) | |||
var hash = new { x:int, y:string }( 3, "foo" ) | |||
var hash = { x:10, y:"foo" } : { x:int, y:string } | |||
=== Objects === | |||
Objects are an unordered collection of properties each of which | |||
contains a primitive value, object, or function. A function stored in a property of an object is called a method. | |||
== | Internally speaking, objects are a kind of [http://en.wikipedia.org/wiki/Associative_array associative array]s or whatever they are called (e.g. records or hash structures). In fact, | ||
arrays are a special case of objects, i.e. an array is defined by the global JS object [http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:prototype Array.prototype] | |||
Anyhow, the following should be internally the same or mostly (?) the same. | |||
<pre> | |||
var take1 = {'prop1':0, 'prop2':1, 'prop2':2}; | |||
var take2 = new Array(); | |||
take2['prop1'] = 0; | |||
take2['prop2'] = 1; | |||
take2['prop3'] = 2; | |||
var take3 = new Object(); | |||
take3.prop1 = 0; | |||
take3.prop2 = 1; | |||
take3.prop3 = 2; | |||
</pre> | |||
However, <code>take2['prop2'] = 0;</code> only works if prop2 already was defined or if you use some ugly eval trick like in the following example | |||
var index = "prop1"; | |||
eval("imageStore." + index + "= new Image()"); | |||
== Links == | |||
=== Manuals and specifications=== | |||
; Flash 9/ActionScript 3 | |||
* [http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/js/html/wwhelp.htm?href=00001826.html Data type descriptions] @ Adobe livedocs | * [http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/js/html/wwhelp.htm?href=00001826.html Data type descriptions] @ Adobe livedocs | ||
; JavaScript 1.5 | |||
* [http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array Core JavaScript 1.5 Reference:Global Objects:Array] | |||
; ECMAScript | |||
* [http://www.mozilla.org/js/language/E262-3.pdf ECMAScript Language Specification] (this is something normal people like you and me have difficulty understanding) | |||
=== JavaScript Tutorials === | |||
* [http://www.quirksmode.org/js/associative.html Objects as associative arrays] | |||
[[Category: Actionscript 3]] | [[Category: Actionscript 3]] | ||
[[Category: JavaScript]] | |||
[[Category: Tutorials]] | [[Category: Tutorials]] |
Revision as of 15:56, 14 April 2008
<pageby nominor="false" comments="false"/>
For now, this is a simple outline to keep track of the concepts that are assumed to have been described.
Introduction
For now we describe the data types you need to write basic programs.
Primitive Data Types
Boolean, int, Null, Number, String, uint, and void.
Numbers
Number. Data type representing any sort of number. int. Integer. Data type representing a number with no decimal part. uint. Unsigned integer. Data type representing a non negative integer
Strings
A variable of string data types represent a sequence of characters.
Boolean
Variables defined as of boolean type can hold only two values: true or false. No other values are valid. When a boolean variable is declared without being given an initial value, its value is false.
Null
The Null data type contains only one value, null
void
The void data type contains only one value, undefined.
Complex data types in ActionScript core classes
The full list of ActionScript 2 datatypes include: Object, Array, Date, Error, Function, RegExp, XML, and XMLList.
Arrays
To create an array and to add some values, you can use something like:
var mylist = Array (); mylist[]=1; mylist[]=2; mylist[]=3;
or
var mylist = [1,2,3];
You can define the lenght of an array:
var mylist = new Array(12);
But it then can be increased later, e.g. this is ok:
mylist(14) = "bla";
There are several sorts of arrays:
- normal arrays (as seen above)
var xx = [1,2,3]
But then we also can have
- typed arrays (ECMAScript 4 / JavaScript 2 - not tested !)
var xx = [ int] (12) // an array of 13 integer elements var xx = [1,2,3] : [int] // an array filled with 2 int. elements var xx = new [int] (1,2,3) // same
- associative arrays (records)
var hash = { x:10, y:"foo" }
- This will return 10:
alert(hash ['x'])
- typed associative arrays (ECMAScript 4 / JavaScript 2 - not tested !)
var hash = new { x:int, y:string }( 3, "foo" ) var hash = { x:10, y:"foo" } : { x:int, y:string }
Objects
Objects are an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.
Internally speaking, objects are a kind of associative arrays or whatever they are called (e.g. records or hash structures). In fact, arrays are a special case of objects, i.e. an array is defined by the global JS object Array.prototype
Anyhow, the following should be internally the same or mostly (?) the same.
var take1 = {'prop1':0, 'prop2':1, 'prop2':2}; var take2 = new Array(); take2['prop1'] = 0; take2['prop2'] = 1; take2['prop3'] = 2; var take3 = new Object(); take3.prop1 = 0; take3.prop2 = 1; take3.prop3 = 2;
However, take2['prop2'] = 0;
only works if prop2 already was defined or if you use some ugly eval trick like in the following example
var index = "prop1"; eval("imageStore." + index + "= new Image()");
Links
Manuals and specifications
- Flash 9/ActionScript 3
- Data type descriptions @ Adobe livedocs
- JavaScript 1.5
- ECMAScript
- ECMAScript Language Specification (this is something normal people like you and me have difficulty understanding)