You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Feature Request: add a new static method to classes exposed to Python to instantiate JavaScript classes.
Code example:
importpythonmonkeyaspmJsDateClass=pm.DatemyDate=JsDateClass.new(0) # in JS this would look like: myDate = new JsDateClass(0);
Relevant Context
Unlike JavaScript, Python doesn't use the new keyword (or anything like it) for instantiating classes. For example:
classSomeClass:
passobj=SomeClass()
However, JavaScript "classes" may require the new keyword to properly setup the object. ES6 classes will throw an error if not instantiated with new.
So we needed to provide a way for Python to instantiate JavaScript classes and chose to add a new function to the PythonMonkey api which returns a function wrapper for the constructor function which can be used to instantiate the class as if it were called with new. See the relevant ticket here: #144
Activity
WebReflection commentedon Sep 15, 2023
FWIWI MicroPython in WASM also supports this syntax and it's great to see alignment around this tiny, yet huge, detail.
Thank You!
wesgarland commentedon Sep 19, 2023
@WebReflection Do you have any insight as to the implementation details in other platforms? A couple of ways to do this pop in my mind right away:
Function.prototype.newin JS-landnewonJSFunction *from PythonI'm sort of leaning toward #1 due to simplicity, but it would have observable side effects in JS.
wiwichips commentedon Sep 28, 2023
Hi @zanxueyan, sorry to hear you're having issues. Let's discuss this in the other thread: #170