Ru | En
How to create a class in Drakon.Tech
- Create a folder inside a module, so that the folder name will look like class ClassName. For example, if we want to create a class named Foo, we need to create a folder with name class Foo.
- The functions in this new folder will become the methods of class Foo. In our example, the Foo class will have one method hello.
How to use classes
For each class, Drakon.Tech will generate a factory function. The factory function creates and returns an instance of the class.
obj = Foo(); obj.hello();
Do not use the new keyword to create class instances.
The self object is availabe in class methods. self is a reference to the class instance. Do not use this keyword to access the class instance.
The factory function adds methods to the class instance, but not fields. The fields need to be added separately. In this example, we put name field in a Foo object.
Class methods can be used as delegates. We can save a method in a variable and call it in other place. There is no need to pass around self object.
action = obj.hello; action();