Both sides previous revision Previous revision Next revision | Previous revision |
guides:objects:classes [2025-03-31 19:34] – [Function Objects] Rename to "Functions as Objects" geek | guides:objects:classes [2025-08-05 12:55] (current) – [Class Syntax] Fix a missing "as" geek |
---|
} | } |
| |
| MyVar := MyFunction ; Put MyFunction into a different variable |
| |
| ; MsgBox is an Object and a Function |
MsgBox IsObject(MsgBox) ", " Type(MsgBox) | MsgBox IsObject(MsgBox) ", " Type(MsgBox) |
| |
| ; MyFunction is an Object and a Function |
MsgBox IsObject(MyFunction) ", " Type(MyFunction) | MsgBox IsObject(MyFunction) ", " Type(MyFunction) |
| |
MyVar := MyFunction ; Put MyFunction into a different variable | ; MyVar, which contains MyFunction, is also now an object and a function |
MyVar() ; Call the function object stored inside MyVar | MsgBox IsObject(MyVar) ", " Type(MyVar) |
| |
| ; We can call the function inside MyVar easily |
| MyVar() |
</runner> | </runner> |
| |
AutoHotkey's ''class'' syntax is so-called [[https://en.wikipedia.org/wiki/Syntactic_sugar|sugar syntax]]. Sugar syntax is an easier to read and write shorthand for code that is too verbose to work with directly. The implication of calling class syntax as sugar syntax is that you can do almost everything that the ''class'' keyword does entirely without using it. There are a few minor exceptions that we will go over later. | AutoHotkey's ''class'' syntax is so-called [[https://en.wikipedia.org/wiki/Syntactic_sugar|sugar syntax]]. Sugar syntax is an easier to read and write shorthand for code that is too verbose to work with directly. The implication of calling class syntax as sugar syntax is that you can do almost everything that the ''class'' keyword does entirely without using it. There are a few minor exceptions that we will go over later. |
| |
Class syntax is used to simultaneously define two things: a "prototype object" and a "class object". A prototype object is used the *base* object for class instances. When you create an object like ''myObject := MyClass()'', the value of ''myObject'' ends up looking something like ''myObject := {base: MyClass.Prototype}''. The prototype object is the object that holds all the method functions that you can call on the class instance. | Class syntax is used to simultaneously define two things: a "prototype object" and a "class object". A prototype object is used as the *base* object for class instances. When you create an object like ''myObject := MyClass()'', the value of ''myObject'' ends up looking something like ''myObject := {base: MyClass.Prototype}''. The prototype object is the object that holds all the method functions that you can call on the class instance. |
| |
Remembering the fundamental of how functions stored in objects are called, it would mean that in this following example, when ''testMethod'' is called the value of ''this'' will be equal to ''myObject'' //not// ''MyClass.Prototype''. | Remembering the fundamental of how functions stored in objects are called, it would mean that in this following example, when ''testMethod'' is called the value of ''this'' will be equal to ''myObject'' //not// ''MyClass.Prototype''. |