JavaScript namespace
See original GitHub issueOne thing I like about Python, is that it has namespaces and variables don’t just come out of nowhere.
Here is example:
__pragma__('alias', 'jQuery', '$')
def start():
def changeColors(divs):
for div in divs:
rgb = [int(256 * Math.random()) for i in range(3)]
jQuery(div).css({'color': 'rgb({},{},{})'.format(*rgb)})
changeColors(jQuery('div'))
window.setInterval(changeColors, 500)
This code has 4 undefined variables, that comes out of nowhere. All Python static syntax checkers will issue number of errors, for example:
> pyflakes jquery.py
jquery.py:1: undefined name '__pragma__'
jquery.py:6: undefined name 'Math'
jquery.py:7: undefined name 'jQuery'
jquery.py:9: undefined name 'jQuery'
jquery.py:10: undefined name 'window'
Having a namespace that represents JavaScript global namespace would solve this issue, for example:
import __transcrypt__
import __javascript__ as js
__transcrypt__.alias('jQuery', '$')
def start():
def changeColors(divs):
for div in divs:
rgb = [int(256 * js.Math.random()) for i in range(3)]
js.jQuery(div).css({'color': 'rgb({},{},{})'.format(*rgb)})
changeColors(js.jQuery('div'))
js.window.setInterval(changeColors, 500)
Now, there is no undefined variables and it is clear what comes from JavaScript or Transcrypt and what is defined in Python modules.
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
JavaScript | Namespace - GeeksforGeeks
Namespace refers to the programming paradigm of providing scope to the identifiers (names of types, functions, variables, etc) to prevent ...
Read more >How do I declare a namespace in JavaScript? - Stack Overflow
In JavaScript there are no predefined methods to use namespaces. In JavaScript we have to create our own methods to define ...
Read more >Namespaces in JavaScript - Flavio Copes
Namespacing is the act of wrapping a set of entities, variables, functions, objects under a single umbrella term. JavaScript has various ways to...
Read more >JavaScript Namespace - Linux Hint
In JavaScript, the concept of adding classes, methods, variables, and objects inside a container is known as “namespace“. The code you write in...
Read more >How do I declare a namespace in JavaScript? - Tutorialspoint
In JavaScript, isolation, and protection from other JavaScript code when working on web applications are provided by the JavaScript namespace.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I like the elegance of your idea but, after a realy hard try, haven’t succeeded in coming up with a good implementation. As for satisfying the static checker (pyflakes, part of the distro, activated with -c switch), you can use:
Nice trick, thanks.