GJS Legacy Class Syntax

Prior to the introduction of ES6 classes in GJS 1.50, GJS had its own implementation of classes and interfaces to interact with classes. This was implemented in the imports.lang module via imports.lang.Class and imports.lang.Interface.

const Lang = imports.lang;

var A = new Lang.Class({
    Name: 'A',
    GTypeName: 'A',
    Signals: {},
    InternalChildren: [],
    Children: [],
    Extends: GObject.Object,
    _init(a, b) {
      this.parent(a, b);
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13

Comparison between legacy and ES6

Implementations

Legacy ES6
var A = new Lang.Class({
   GTypeName: 'A',
   Name: 'A',
   Extends: GObject.Object,
   _init(a, b) {
       this.parent(a);
       this.b = b;
   }
 });
1
2
3
4
5
6
7
8
9
var A = GObject.registerClass({
       GTypeName: 'A',
   }, class A /* ... */
      /* ... */ extends GObject.Object { 
          constructor(a, b) {
             super(a);
             this.b = b;
         }
   }
);
1
2
3
4
5
6
7
8
9
10

Binding

Legacy ES6
Lang.bind(this, this.myMethod);
1
this.myMethod.bind(this);
1
Lang.bind(this, function () {
    if (this.x) {
        /* ... */
    }
});
1
2
3
4
5
() => {
    if (this.x) {
        /* ... */
    }
}
1
2
3
4
5
Last Updated: 7/21/2022, 3:39:25 AM
Contributors: Andy Holmes, Evan Welsh, Evan Welsh, Javad Rahmatzadeh, Roy Golan