Object

Object is the basic class from which other AutoHotkey object classes derive. Each instance of Object consists of the following:

There are value properties and dynamic properties. Value properties simply contain a value. Dynamic properties do not contain a value, but instead call a getter or setter function when accessed. The getter or setter can be overridden by a derived object (the easiest way is to define them within a class).

The documentation below uses Obj as a placeholder for any instance of Object. All instances of Object are based on Object.Prototype, from which the following predefined methods and properties are inherited:

Methods:

Properties:

Functions:

Methods

Clone

Returns a shallow copy of the object.

Clone := Obj.Clone()

Each property or method owned by the object is copied into the clone. Object references are copied (like with a normal assignment), not the objects themselves; in other words, if a property contains a reference to an object, the clone will contain a reference to the same object.

Dynamic properties are copied, not invoked.

The clone has the same base object as the original object.

DefineMethod

Defines a new method.

Obj.DefineMethod(Name, MethodFunc)
Name

Type: String

The name of the method.

MethodFunc

Type: Function Object

The implementation of the method. The function must accept at least one parameter, which receives a reference to the target object of the method call. This parameter is defined automatically with the name this when the method is created by a class definition.

The new method is owned by Obj, but may be inherited by any objects which use Obj as a base. If Obj is a class, the new method only applies to the class itself and subclasses (i.e. it is static). To define a method for all instances of a class, let Obj be the class's Prototype.

DefineProp

Defines a new dynamic property.

Obj.DefineProp(Name, Desc)
Name

Type: String

The name of the property.

Desc

Type: Object

An object with zero or more of the following own properties:

Get: The function object to call when the property's value is retrieved.

Set: The function object to call when the property is assigned a value. Its second parameter is the value being assigned.

If Get or Set is not defined, it can be inherited from a base object. If Get is undefined, the property can return a value inherited from a base. If Set is undefined in this and all base objects, the property is read-only (attempting to set the property throws an exception).

As with methods, the first parameter of Get or Set is this (the target object). For Set, the second parameter is value (the value being assigned). These parameters are defined automatically by method and property definitions within a class, but must be defined explicitly if using normal functions.

The MaxParams and IsVariadic properties of the function objects are evaluated to determine whether the property may accept parameters. If MaxParams is 1 for get or 2 for set and IsVariadic is false or undefined, the property cannot accept parameters; they are instead forwarded to the __Item property of the object returned by get.

DeleteMethod

Removes an own method from the object.

Obj.DeleteMethod(Name)
Name

Type: String

A method name. If the object does not own a method by this name, an exception is thrown.

DeleteProp

Removes an own property from the object.

Obj.DeleteProp(Name)
Name

Type: String

A property name.

Return Value

The value of the removed property (blank if none).

GetMethod

Retrieves the implementation function of a method.

Obj.GetMethod(Name)
Name

Type: String

A method name.

Return Value

Type: Function Object

The implementation of the method. This is either a Func created by a class definition or an object previously passed to DefineMethod.

The method may be owned by the object itself or inherited from a base object.

GetOwnPropDesc

Returns a descriptor for a given property, compatible with DefineProp.

Obj.GetOwnPropDesc(Name)
Name

Type: String

A property name.

Return Value

Type: Object

A new object with zero or more of the following own properties:

Get: The function object which is called when the property's value is retrieved, if defined in Obj.

Set: The function object which is called when the property is assigned a value, if defined in Obj.

Modifying the returned object has no effect on Obj unless DefineProp is called.

HasOwnMethod

Obj.HasOwnMethod(Name)

Returns true if the object owns a method by this name, otherwise false.

HasOwnProp

Obj.HasOwnProp(Name)

Returns true if the object owns a property by this name, otherwise false.

HasBase

Obj.HasBase(BaseObj)

Returns true if BaseObj is a direct or indirect base of Obj, otherwise false. For example, Obj.HasBase(Array.Prototype) is true if Obj is an instance of Array or any derived class. This the same check performed by Obj is Array; however, instances can be based on other instances, whereas is requires a Class.

HasMethod

Obj.HasMethod(Name)

Returns true if the object has a method by this name, otherwise false. The method may be owned by the object itself or inherited from a base object.

HasProp

Obj.HasProp(Name)

Returns true if the object has a property by this name, otherwise false. The property may be owned by the object itself or inherited from a base object.

OwnMethods

Enumerates the object's own methods.

For Name , MethodFunc in Obj.OwnMethods()

Returns a new enumerator. The enumerator is typically passed directly to a for-loop, which calls the enumerator once for each iteration of the loop. Each call to the enumerator returns the next method name and/or implementation function. The for-loop's variables correspond to the enumerator's parameters, which are:

Name

Type: String

The method's name.

MethodFunc

Type: Function Object

The implementation of the method. This is either a Func created by a class definition or an object previously passed to DefineMethod.

OwnProps

Enumerates the object's own properties.

For Name , Value in Obj.OwnProps()

Returns a new enumerator. The enumerator is typically passed directly to a for-loop, which calls the enumerator once for each iteration of the loop. Each call to the enumerator returns the next property name and/or value. The for-loop's variables correspond to the enumerator's parameters, which are:

Name

Type: String

The property's name.

Value

The property's value.

If the property has a getter method, it is called to obtain the value (unless Value is omitted).

Properties

Base

Retrieves or sets the object's base object.

BaseObj := Obj.Base
Obj.Base := BaseObj

BaseObj must be an Object.

If assigning the new base would change the native type of the object, an exception is thrown. An object's native type is decided by the nearest prototype object belonging to a built-in class, such as Object.Prototype or Array.Prototype. For example, an instance of Array must always derive from Array.Prototype, either directly or indirectly.

Properties and methods are inherited from the base object dynamically, so changing an object's base also changes which inherited properties and methods are available.

See also: ObjGetBase, ObjSetBase

Functions

ObjRawGet

Retrieves the value of an object's own property.

Deprecated: This function may be removed or replaced in a future release.

Value := ObjRawGet(Obj, Name)

If the object does not own a property by this name or it is a dynamic property, the return value is an empty string. No meta-functions or property functions are called. Inherited properties are not considered.

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

ObjRawSet

Creates a new value property or sets the value of an existing one.

Deprecated: This function may be removed or replaced in a future release.

ObjRawSet Obj, Name, 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: Obj.%Name% := 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 Obj is of an incorrect type.

ObjGetBase

Returns the object's base object.

BaseObj := ObjGetBase(Obj)

No meta-functions or property functions are called. Calling this function is equivalent to invoking the default implementation of the Base property, even when that property has been overridden.

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

See also: Base property

ObjSetBase

Sets the object's base object.

ObjSetBase(Obj, BaseObj)

No meta-functions or property functions are called. Calling this function is equivalent to invoking the default implementation of the Base property, even when that property has been overridden.

An exception is thrown if Obj or BaseObj is of an incorrect type.

See also: Base property

ObjOwnPropCount

Returns the number of properties owned by an object.

Count := ObjOwnPropCount(Obj)

ObjSetCapacity

Sets the current capacity of the object's internal array of own properties.

ObjSetCapacity(Obj, MaxProps)
MaxProps

Type: Integer

The new capacity. If less than the current number of own properties, that number is used instead, and any unused space is freed.

Return Value

Type: Integer

The new capacity.

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

ObjGetCapacity

MaxItems := ObjGetCapacity(Obj)
Return Value

Type: Integer

The current capacity of the object's internal array of properties.

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