|Written by Ian Elliot|
|Thursday, 27 October 2016|
Page 2 of 2
The function evaluation operator
To evaluate a function you use the () operator:
which executes the functionobject to its left and evaluates to the result that the function returns.
Notice that you can apply the () operator to a function object no matter how you choose to retrieve it so you can use the property notation to make it look like an object method:
or you can use array notation which makes the method call look very odd:
It doesn't matter how odd it looks as the () operator always evaluates the function object to its left and returns the result of the function as its value.
Now we need to look at the special case of integer keys.
As an alternative to string keys you can use integer keys but if you do use a string key you cannot refer to the value using its ordinal position in the array.
For example if you define the array as:
You cannot refer to key1 using array or key2 as array. You can only retrieve values using the key you specified there is no ordinal position in an associative array.
That is if you define:
you can retrieve value2 using
So apart from not being able to use property syntax integer keys are no different. However they look as if they should be different. In particular an associative array with integer keys looks like a standard indexed array - but it isn't.
For example, if we define the array:
then you can call the function using:
Only the array elements you define are created in an associative array and so in this case array has just two members - array and array.
You can use an Array as an associative array but it really isn't a good idea because you are then incurring an overhead you aren't using and it can become very confusing having and indexed store and an associative store.
That is don't use
This is a particular problem when you want to iterate through the array using a for loop:
this loops through all of the properties of the object including those that were derived from Object. The usual solution is to use the hasOwnProperty method which returns true if the key is one you actually defined.
If you are not iterating though an associative array the non-ownProperities are less of a problem and can usually be ignored.
Since ECMAScript 5 you can create an empty associative array using the syntax:
In ECMAScript 6 a new associative array object has been introduced called Map.
Now array is a Map which is an associative array with no entries. Notice that it does have some built-in properties and methods but these are treated separately from any key value pairs you might add. It isn't exactly the same as an empty object however because you use any object or primitive type as the key in a Map. It also has a size property which returns the number of elements stored in it and a few other useful methods.
If you can make use of ECMAScript 6 then you should use Map in place of Object when you need an associative array but with care Object does work well.
DoEvents is generally regarded as a construct to be avoided. Like many such feared ideas it is only to be feared if you don't understand it. Correctly used it can be a simplification. This extra [ ... ]
Closures are logical but subtle and you need to make sure that you really understand them. This is made easier if you think about a closure as a preserved execution context. In this extract from [ ... ]
or email your comment to: firstname.lastname@example.org