how can I use "lang" in gjs?
Asked Answered
F

1

3

I'm working on Gnome shell extension recently. I looked at some code, like this:

const Lang = imports.lang;

const extension = new Lang.Class({...})

I can't find any information about Lang in GJS.

Where should I go to find the relevant development manual?

Fattish answered 13/7, 2021 at 5:25 Comment(1)
Does this answer your question? Lang.Class in JavascriptDoug
E
6

Don't use Lang anymore; it's deprecated and there are better ways. It was created before Function.prototype.bind() and ES6 Classes. Some reading:

Signal Callbacks

// NOTE: the emitting object is always the first argument,
//       so `this` is usually bound to a different object.
function myCallback(sourceObject, arg1) {
    if (this === sourceObject)
        log('`sourceObject` is correctly bound to `this`');
}

// OLD
sourceObject.connect('signal-name', Lang.bind(myCallback, sourceObject));

// NEW
sourceObject.connect('signal-name', myCallback.bind(sourceObject));

GObject Classes

// OLD
const MyLegacyClass = new Lang.Class({
     GTypeName: 'MyLegacyClass',
     Extends: GObject.Object,
     _init(a, b) {
         this.parent(a);
         this.b = b;
     }
});

// NEW
const MyClass = GObject.registerClass({
     GTypeName: 'MyLegacyClass',
}, class MyClass extends GObject.Object { 
     _init(a, b) {
         super._init(a);
         this.b = b;
     }
);
Euchologion answered 13/7, 2021 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.