Skip to content

[Suggestion] A special function to create JS classes #2548

Open
@denis-migdal

Description

@denis-migdal

Hi,

Currently, when a Python class extends a JS class, it doesn't really inherit from it.

This may lead to some issues when such class is used in JS code, e.g. trying to extend an existing JS WebComponent.

We can dev some workaround with JS/Py helper functions. But this can be quite complex, playing with internal functions ($call, $getattr), etc.

I suggest to add a JSKlass() function that would allow to create real JS classes in Brython, and use it as a normal JS class (so requiring internally to $B.jsobj2pyobj() it to then use it in the Brython code).

It could be used like that:

class MyClass(javascript.JSKlass(window.HTMLElement)):
    def foo(self, a):
         pass

    prop = 3

That would be written as something like:

MyClass = $B.jsobj2pyobj( class MyClass extends HTMLElement {

     $BRY_DEF = {}; // the brython "implementation" of the class (mainly the methods ?).

     prop = $Bpyobj2jsobj(3);

     foo(...args) {
         return $B.call( this.$BRY_DEF.foo, [0,0,0])( ...args ); // I think this needs some pyobj2jsobj and jsobj2pyobj
     }
});

Ofc, there would be some restrictions on what we are allowed to do in such class.
But i think this would be quite helpful.

Cordially,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions