HOWTO use loadable modules in LianjaScript
by
, 2021-11-09 at 21:49 (39184 Views)
LianjaScript Modules are similar to libraries in that they can be dynamically loaded and contain procedures that may be used in your Apps.
Modules however encapsulate all procedures, functions and public variables and make these only visible from an object. This prevents name clashes and is a simple way to wrap code libraries into objects without any special coding syntax.
Modules provide simple (Object-Oriented Programming ) OOP concepts:
- Objects
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
e.g.
You dynamically load a module using the require() built-in functionCode:// file: mylib1.prg public myvar = “Barry” proc getMyVar() return myvar
You can now access the public variables and call procs like this.Code:public mylib = require(“mylib1”)
Modules also handle inheritanceCode:value = mylib.myvar value = mylib.getMyVar()
Now dynamically load the modulesCode:// file: mylib2.prg public myvar = “Bill” proc getMyVar() return myvar
You can now access the public variables and call procs like this.Code:public mylib = require(“mylib1”, “mylib2”) // , “mylib3”… etc
Modules can optionally contain an init() initialization proc which is called after the module is loaded.Code:value = mylib.myvar value = mylib.getMyVar()
Note that the filenames specified as arguments to the require() function may contain the special filename prefixes such as lib:/ and thirdparytlibs:/ etcCode:// file: mylib3 public myvar = “Bill” proc init() myvar = “Tom” endproc proc getMyVar() return myvar
Lianja also supports JavaScript dynamically loadable modules in both desktop and web apps. This will be covered in another article.
Prerequisites
Lianja 6.3