Interface
The functional interface of So-o in JavaScript consists of 3 functions:
defclass
which defines a new class, sendmsg
which is systematically used to send a message to a class or an instance, and supersend
which runs a method inherited from a superclass.
All the code for the So-o interface is in the file So-o.js.
- import 'Root';
Loading the code for So-o automatically includes the Root class.
defclass
SYNOPSIS
defclass(name, superclass, revision, classProperties, instanceProperties, classMessages, instanceMessages)
DESCRIPTION
defclass
defines a class.
name
specifies the name of the class. name
must be a valid JavaScript variable name.
superclass
is the global reference of the superclass of the new class.
If superclass
is null
, the new class inherits by default from the Root class, Root
.
revision
gives the revision number of the class. revision
is an integer > 0 which can be used to differentiate successive versions of the class.
classProperties
and instanceProperties
list the properties of the class and instances of the class.
A property is a character string.
classMessages
and instanceMessages
lists of messages and methods of the class and instances of the class.
A message is a character string.
A method is the code of a function.
classProperties
, instanceProperties
, classMessages
and instanceMessages
can be null
.
defclass
creates the global variable name
.
Notice that a class can be redefined.
A new class automatically receives the message initialize.
NOTE: The initialize
class method defined by the Root class does nothing.
EXAMPLE
- import { defclass } from 'So-o';
Imports the function defclass
of So-o.
The file So-o.js automatically includes the Root class.
- defclass('Hello', null, 1,
- null,
- null,
- null,
- { 'hello': (self) => {
- console.log('Hello from So-o!');
- return self;
- }
- }
- );
Defines the class Hello and associates it to the global variable Hello
.
The Hello class inherits by default from the Root class.
Its revision number is 1.
It doesn't have any class properties, class messages or instance properties.
It has an instance message: hello.
The first argument of a method is always the class or the instance which receives the message.
Conventionally, this argument is called self
.
A method which has nothing in particular to return generally returns self
.
A few lines of code to illustrate the use of the Hello
class:
- import { sendmsg } from 'So-o';
- import 'Hello';
- var hello = sendmsg(Hello, 'new');
- sendmsg(hello, 'hello');
Imports the function sendmsg
of So-o.
Imports the class Hello.
Sends the message new to the class Hello to create the instance hello
assigning to it the value returned by sendmsg
.
Sends the message hello to the instance hello
to display the welcome message.
$ ln Hello.js node_modules/Hello.mjs
$ ln testHello.js testHello.mjs
$ nodejs --experimental-modules testHello
Hello from So-o!
CODE
- import { Definition } from 'OL';
Imports the prototype Definiton
of the Object Layer.
- export function defclass(name, superclass, revision, classProperties, instanceProperties, classMessages, instanceMessages) {
defclass
takes 7 arguments.
name
specifies the name of the class.
superclass
is the global reference of the superclass of the new class.
revision
gives the revision number of the class.
classProperties
, instanceProperties
, classMessages
and instancMessages
list the properties and the messages of the class and the instances of the class.
- let c = new Definition(name, superclass, revision, classProperties, instanceProperties, classMessages, instanceMessages);
Creates an object of the type Definition defined by the Object Layer with the parameters of the call to defclass
.
- global[name] = c;
Assigns the class to a global variable whose name is the name of the class.
- if ('Root' !== name)
- sendmsg(c, 'initialize');
Sends the message initialize to the new class if it's not the Root class.
- return c;
- }
Returns the class.
sendmsg
SYNOPSIS
sendmsg(rcv, msg[, arg ...])
DESCRIPTION
sendmsg
returns the result of sending the message msg
and its parameters arg
to the instance or the class rcv
.
CODE
- export function sendmsg(rcv, msg, ...args) {
- return rcv.sendself(msg, args);
- }
sendmsg
calls the function sendself
of rcv
with in argument msg
and an array containing all the other arguments following msg
.
supersend
SYNOPSIS
sendmsg(fc, rcv, msg[, arg ...])
DESCRIPTION
supersend
returns the result of sending the message msg
and its parameters to an instance or a class in the context of the superclass of the class fc
.
supersend
is generally used to call the inherited version of a method which is redefined by a subclass.
In this case, c
is the class of the method which redefines msg
and calls supersend
.
CODE
- export function supersend(fc, rcv, msg, ...args) {
- return rcv.sendsuper(fc, msg, args);
- }
supersend
calls the function sendsuper
of rcv
with in argument fc
, msg
and array containing all the arguments following msg
.
EXAMPLE
- import { defclass, sendmsg, supersend } from 'So-o';
Imports the functions defclass
, sendmsg
and supersend
from So-o.
- defclass('X', null, 1,
- ['count'],
- ['value'],
The X class inherits from the Root class, adds the class property count and the instance property value, redefines the class messages initialize and new, adds the class message count, redefines the instance messages init and free.
- { 'new': (self, ...args) => {
- let i = supersend(X, self, 'new', ...args);
- sendmsg(self, 'set', 'count', sendmsg(self, 'get', 'count') + 1);
- return i;
- },
new
creates an instance of X by calling the method new inherited from the superclass of X with all the parameters of the call. NOTE: The new method of the Root class passes its arguments to the instance method init which is redefined by the X class.
- 'initialize': (self) => sendmsg(self, 'set', 'count', 0),
initialize sets the class property count to 0. NOTE : initialize is called once automatically by defclass
.
- 'count': (self) => sendmsg(self, 'get', 'count')
- },
count returns the valeur of the class property count.
- { 'init': (self, value = 0) => {
- supersend(X, self, 'init');
- sendmsg(self, 'set', 'value', value);
- return self;
- },
init calls the init method inherited from the superclass of X and initializes the instance property value.
- 'free': (self) => {
- let count = sendmsg(sendmsg(self, 'class'), 'get', 'count');
- sendmsg(sendmsg(self, 'class'), 'set', 'count', count -1);
- supersend(X, self, 'free');
- }
- }
- );
free decrements by 1 the counter of instances of the X class and calls the free method inherited from the superclass of X.
- import { sendmsg } from 'So-o';
- import 'X';
- console.log(sendmsg(X, 'count'));
Imports the function sendmsg
from So-o.
Imports the X class. Displays the instance counter of the X class.
- let x1 = sendmsg(X, 'new');
- console.log(sendmsg(x1, 'get', 'value'));
Creates an instance of X with the default value. Displays the value of the instance.
- let x2 = sendmsg(X, 'new', 1);
- console.log(sendmsg(x2, 'get', 'value'));
Creates an instance of X with the value 1. Displays the value of the instance.
- console.log(sendmsg(X, 'count'));
Displays the instance counter of the X class.
- sendmsg(x1, 'free');
- x1 = undefined;
- console.log(sendmsg(X, 'count'));
Frees an instance of X. Displays the instance counter of the X class.
$ ln X.js node_modules/X.mjs
$ ln testX.js test.mjs
$ nodejs --experimental-modules testX
0
0
1
2
1
Comments