menu
Menu
Drakon.Tech logo
Drakon.Tech
Get started
Drakon.Tech documentationProgramming in DRAKONThe basics of programming with Drakon.TechHello worldVariables and functionsif-else | Question iconswitch-case | the Choice iconThe foreach loopThe arrow (while) loopSilhouetteClassesLambdas and exceptionsAdvanced programming methodsGame examplesThe source codeLegacy tutorials (JavaScript 0.2)

Classes

Ru | En

Source code: Class

How to create a class in Drakon.Tech

  1. 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.
  2. 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 create a class in Drakon.Tech

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.

A class method and self keyword in Drakon.Tech

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.

How to create an instance of a class in Drakon.Tech

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();
Source code: Class
close
Close
Drakon.Tech logo
Drakon.Tech home
Programming in DRAKONThe basics of programming with Drakon.TechAdvanced programming methodsThe source codeLegacy tutorials (JavaScript 0.2)