gnomegnome-shellgjs

how can I use "lang" in gjs?


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?


Solution

  • 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;
         }
    );