Open wiwichips opened 9 months ago
FWIWI MicroPython in WASM also supports this syntax and it's great to see alignment around this tiny, yet huge, detail.
Thank You!
@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.new
in JS-landnew
on JSFunction *
from PythonI'm sort of leaning toward #1 due to simplicity, but it would have observable side effects in JS.
Hi @zanxueyan, sorry to hear you're having issues. Let's discuss this in the other thread: https://github.com/Distributive-Network/PythonMonkey/issues/170
Describe your feature request here.
Class.new
Feature Request: add a
new
static method to classes exposed to Python to instantiate JavaScript classes. Code example:Relevant Context
Unlike JavaScript, Python doesn't use the
new
keyword (or anything like it) for instantiating classes. For example:However, JavaScript "classes" may require the
new
keyword to properly setup the object. ES6 classes will throw an error if not instantiated withnew
.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 withnew
. See the relevant ticket here: https://github.com/Distributive-Network/PythonMonkey/issues/144This topic was debated in the TC39 forums here: https://es.discourse.group/t/function-prototype-new/1772 ; however, the community does not appear to be very unified on this subject. @WebReflection suggested adding
Class.new
to the JS specPyodide uses
Class.new
to solve this, refer to the Pyodide documentation here: https://pyodide.org/en/stable/usage/api/python-api/ffi.html#pyodide.ffi.JsProxy.newCode example