Transcrypt: Inherit from JS prototype?

Is there a way to inherit the Transcrypt class from the JS prototype? I have a JS object type that has a rather heavy set of features that I need to keep, but I would like to extend it with a lot of advantages in the Transcrypt class (in particular, I would like to add awkward JS math functions with Transcript operator overloading) ...

I've tried the obvious:

class MyClass (MyJSClass):
    ....

      

But that doesn't work because the JS class doesn't have transcriptive "magic methods".

I also tried adding methods to the JS prototype:

def add_repr(orig):
    def v_repr(self):
        return "(inherited JS Object)"
    orig.prototype.__repr__ = v_repr

add_repr(MyJSClass)

print (__new__(MyJSClass()))

      

but in this case it repr

will never be called because Transcrypt looks for other magic methods or identifiers and does not find them so that it does not lookrepr

Has anyone devised a method to retroactively turn a JS prototype into a Transcrypt class, or to inherit a Transcrypt class from a JS prototype?

+3


source to share


1 answer


As you noticed, Transcrypt classes differ from each other across JavaScript classes due to their support for multiple inheritance and the assignment of related functions.

A clean solution is to create a Transcrypt facade class that encapsulates the corresponding JavaScript class.

So, in order for the Transcrypt class Y_tr

(and other classes) to inherit from the JavaScript class X_js

, define the Transcrypt class X_tr

with the X_js

class object X_js

as its only attribute (generated X_tr.__init__

). Then you can Y_tr

inherit from X_tr

instead of X_js

.



Say X_js

has methods m_a

and m_b

, then give X_tr

methods with that name as well. The method X_tr.m_a

just calls x_js.m_a

and X_tr.m_b

just calls x_js.m_b

, returning the appropriate results.

Attributes X_js

can be accessed through properties of X_tr

the same name.

Note that X_tr

and Y_tr

can be created without using __new__

, since it self.x_js = __new__ (X_js ())

is executed in X_tr.__init__

.

+1


source







All Articles