PiotrDabkowski / Js2Py

JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀 Try it online:
http://piter.io/projects/js2py
MIT License
2.47k stars 261 forks source link

JS function class to python #290

Open silverbasilisk opened 2 years ago

silverbasilisk commented 2 years ago

Hello @PiotrDabkowski I've translated this javascript file to python and got the XX object.

** JavaScript **
XX.WSVerifyCookieReq = function () {
    this.lUid = 0;
    this.sUA = "";
    this.sCookie = "";
    this.sGuid = "";
    this.bAutoRegisterUid = 0
};

Can I declare a new WSVerifyCookieReq class which is equivalent to this?

** JavaScript **
verifyReq = new XX.WSVerifyCookieReq();
verifyReq.sUA = "new UA";

I tried like this but it's causing error.

        verifyCookieReq = XX.WSVerifyCookieReq()
        verifyCookieReq.sUA = "new UA"
worstperson commented 1 year ago

Sorry, I didn't understand at first. I think you can implement it like this in python:

import js2py

context = js2py.EvalJs()  
context.execute('''
var XX = [];
XX.WSVerifyCookieReq = function () {
    this.lUid = 0;
    this.sUA = "";
    this.sCookie = "";
    this.sGuid = "";
    this.bAutoRegisterUid = 0
};
''')

verifyReq = context.XX.WSVerifyCookieReq._obj.create().to_python()
# Or verifyReq = context.eval("new XX.WSVerifyCookieReq()")
verifyReq.sUA = "new UA"