如何使 Transcrypt 编译为对象而不是字典?

How can one make Transcrypt compile to object rather than dict?

我正在尝试使用来自 Transcrypt 的提取 API。

来源:

headers = {
    "Access-Control-Request-Method": "GET",
    "Access-Control-Request-Headers": "origin, x-requested-with",
    "Origin": "https://bar.com/"
}

args = {
    "method":"OPTIONS",
    "headers":headers
}

fetch("https://foo.com/",args).then(
    lambda response: print(response),
    lambda err: print(err)
)

编译为:

var headers = dict ({'Access-Control-Request-Method': 'GET', 'Access-Control-Request-Headers': 'origin, x-requested-with', 'Origin': 'https://bar.com/'});
        var args = dict ({'method': 'OPTIONS', 'headers': headers});
        fetch ('https://foo.com/', args).then ((function __lambda__ (response) {
            return print (response);
        }), (function __lambda__ (err) {
            return print (err);
        }));

打印:

TypeError: Failed to execute 'fetch' on 'Window': Iterator is not an object.

如果我从编译代码中删除 dicts,它就会运行。

如何编译成正确的 Javascript 对象而不是字典?

知道了。

事实证明:

https://www.transcrypt.org/docs/html/special_facilities.html#create-bare-javascript-objects-and-iterate-over-their-attributes-from-python-pragma-jsiter-and-pragma-nojsiter

Normally a Python {...} literal is compiled to dict ({...}) to include the special attributes and methods of a Python dict, including e.g. an iterator. When pragma ('jsiter') is active, a Python {...} literal is compiled to a bare {...}, without special attributes or methods. To still be able to iterate over the attributes of such a bare JavaScript object from Python, when pragma ('jsiter') is active, a Python for ... in ... is literally translated to a JavaScript for (var ... in ...). The main use case for this pragma is conveniently looping through class attributes in the new method of a metaclass. As a more flexible, but less convenient alternative, pragma ('js', '{}', '''...''') can be used.

所以代码应该是:

__pragma__('jsiter')

headers = {
    "Access-Control-Request-Method": "GET",
    "Access-Control-Request-Headers": "origin, x-requested-with",
    "Origin": "https://bar.com/"
}

args = {
    "method":"OPTIONS",
    "headers":headers
}

__pragma__('nojsiter')