如何在 gjs 中使用“lang”?

Jas*_* ap 3 gnome gnome-shell gjs

我最近正在研究 Gnome shell 扩展。我查看了一些代码,如下所示:

\n
const Lang = imports.lang;\n\nconst extension = new Lang.Class({...}\xef\xbc\x89\n
Run Code Online (Sandbox Code Playgroud)\n

我在GJS中找不到任何有关Lang的信息。

\n

相关的开发手册应该去哪里找?

\n

and*_*mes 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));
Run Code Online (Sandbox Code Playgroud)

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;
     }
);
Run Code Online (Sandbox Code Playgroud)