Object

AutoHotkey's basic object datatype is an associative array with features which allow its behaviour to be customized. By default, all objects created by {}, [], Object() and Array() support the following:

Each method also has an equivalent function, which can be used to bypass any custom behaviour implemented by the object -- it is recommended that these functions only be used for that purpose. To call one, prefix the method name with "Obj" and pass the target object as the first parameter. For example:

array := [1, 2, 3]
MsgBox % ObjMaxIndex(array) " = " array.MaxIndex()

If an Obj method-function is called with an object or value of the wrong type, it returns an empty string. Standalone functions such as ObjRawSet throw an exception.

InsertAt [v1.1.21+]

Inserts one or more values at a given position within a linear array.

Object.InsertAt(Pos, Value1 , Value2, ... ValueN)

Pos

The position to insert Value1 at. Subsequent values are inserted at Pos+1, Pos+2, etc.

Value1 ...

One or more values to insert. To insert an array of values, pass theArray* as the last parameter.

Remarks

As Objects are associative arrays, Pos is also the integer key which will be associated with Value1. Any items previously at or to the right of Pos are shifted to the right by the exact number of value parameters, even if some values are missing (i.e. the object is a sparse array). For example:

InsertAt should be used only when the object's integer keys represent positions in a linear array. If the object contains arbitrary integer keys such as IDs or handles, InsertAt is likely to cause unwanted side-effects. For example:

InsertAt does not affect string or object keys, so can be safely used with objects containing mixed key types.

RemoveAt [v1.1.21+]

Removes items from the given position in a linear array.

Object.RemoveAt(Pos , Length)

Pos

The position of the value or values to remove.

Length

The length of the range of values to remove. Items from Pos to Pos+Length-1 are removed. If omitted, one item is removed.

Return Value

If Length is omitted, the value removed from Pos is returned (blank if none). Otherwise the return value is the number of removed items which had values, which can differ from Length in a sparse array, but is always between 0 and Length (inclusive).

RemoveAt should be used only when the object's integer keys represent positions in a linear array. If the object contains arbitrary integer keys such as IDs or handles, RemoveAt is likely to cause unwanted side-effects. For example:

RemoveAt does not affect string or object keys, so can be safely used with objects containing mixed key types.

Push [v1.1.21+]

Appends values to the end of an array.

Object.Push( Value, Value2, ..., ValueN)

Value ...

One or more values to insert. To insert an array of values, pass theArray* as the last parameter.

Return Value

The position of the last inserted value. Can be negative if the array only contained elements at negative indices.

Remarks

The first value is inserted at position 1 if the array is empty or contains only string or object keys.

Otherwise, the first value is inserted at Object.MaxIndex() + 1, even if that position is negative or zero. If this is undesired and the object can contain negative keys, Object.InsertAt(Object.Length() + 1, ...) can be used intead.

Pop [v1.1.21+]

Removes and returns the last array element.

Value := Object.Pop()

If there are no array elements, the return value is an empty string. Otherwise, it is equivalent to the following:

Value := Object.RemoveAt(Object.Length())

Delete [v1.1.21+]

Removes key-value pairs from an object.

Object.Delete(Key)
Object.Delete(FirstKey, LastKey)

Key

Any single key.

FirstKey, LastKey

Any valid range of integer or string keys, where FirstKey <= LastKey. Both keys must be the same type.

Return Value

If there is exactly one parameter, the removed value is returned (blank if none). Otherwise the return value is the number of matching keys which were found and removed.

Remarks

Unlike RemoveAt, Delete does not affect any of the key-value pairs that it does not remove. For example:

Count [v1.1.29+]

SetCapacity [AHK_L 31+]

Adjusts the capacity of an object or one of its fields.

Object.SetCapacity(MaxItems)
Object.SetCapacity(Key, ByteSize)

MaxItems

The maximum number of key-value pairs the object should be able to contain before it must be automatically expanded. If less than the current number of key-value pairs, that number is used instead, and any unused space is freed.

Key

Any valid key.

ByteSize

The new size in bytes of the field's string buffer, excluding the null-terminator. If the field does not exist, it is created. If ByteSize is zero, the buffer is freed but the empty field is not removed. If ByteSize is less than the current size, excess data is truncated; otherwise all existing data is preserved.

Returns

The new capacity if successful, otherwise an empty string.

GetCapacity [AHK_L 31+]

MaxItems := Object.GetCapacity()
ByteSize := Object.GetCapacity(Key)

Returns the current capacity of an object or one of its fields.

Returns an empty string if the field does not exist or does not contain a string.

GetAddress [AHK_L 31+]

Ptr := Object.GetAddress(Key)

Returns the current address of the field's string buffer, if it has one.

NewEnum [AHK_L 49+]

Enum := Object._NewEnum()

Returns a new enumerator to enumerate this object's key-value pairs. This method is usually not called directly, but by the for-loop.

HasKey [AHK_L 53+]

Object.HasKey(Key)

Returns true if Key is associated with a value (even "") within Object, otherwise false.

ObjRawGet [v1.1.29+]

Retrieves the value associated with a given key within Object.

Value := ObjRawGet(Object, Key)

If Object does not contain Key, the return value is an empty string. No meta-functions or property functions are called. The content of Object's base objects are not considered, and since base itself is a property and not a key-value pair by default, it is typically not returned.

An exception is thrown if Object is of an incorrect type.

ObjRawSet

Stores or overwrites a key-value pair in the object.

ObjRawSet(Object, Key, Value)

This function is provided to allow scripts to bypass the __Set meta-function and properties. If that isn't required, a normal assignment should be used instead. For example: Object[Key] := Value

Since the purpose is to bypass meta-functions, this is a function only, not a method. Calling a built-in method generally causes the __Call meta-function to be called.

An exception is thrown if Object is of an incorrect type.

ObjGetBase [v1.1.29+]

No meta-functions are called. The object's base is returned even if the key "base" has been stored in the object (such as with ObjRawSet or SetCapacity). An empty string is returned if the object has no base.

ObjSetBase [v1.1.29+]

No meta-functions are called. The object's base is set even if the key "base" has been stored in the object (such as with ObjRawSet or SetCapacity). An empty string is returned if the object has no base.

An exception is thrown if Object is of an incorrect type or if BaseObject is not an object or empty string.