question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JavaScript namespace

See original GitHub issue

One 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:closed
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JdeHcommented, Mar 11, 2016

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:

 __pragma__ ('skip')
window = Math = Date = 0    # Prevent complaints by optional static checker
__pragma__ ('noskip')
0reactions
JdeHcommented, Mar 15, 2016

Nice trick, thanks.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found