def get_jsname(self, name):
        if isinstance(name, Code):
            return str(name)
        jsvars = self.jsvars.copy()
        jsvars.update(name=name)
        jsname = None
        if name in self.names[-1]:
            if self.names[-1][name].to_js is not None:
                return self.names[-1][name].to_js
            if not self.names[-1][name].glob:
                if isinstance(self.names[-1], ClassNames):
                    jsname = """\
%(funcbase)s['$dict'][%(name)r]""" % jsvars
                else:
                    jsname = "%(locals)s[%(name)r]" % jsvars
                jsvars.update(jsname=jsname)
        else:
            for names in self.names[1:-1]:
                if isinstance(names, ClassNames):
                    continue
                if name in names:
                    if isinstance(self.names[-1], ClassNames):
                        cn = self.names.pop()
                    else:
                        cn = None
                    self.add_name(name)
                    jsname = self.get_jsname(name)
                    if cn is not None:
                        self.names.append(cn)
                    return jsname
        if jsname is None:
            if name in ['globals', 'locals']:
                src = "%%(%s)s" % name
                return ("%(builtin)s['%(name)s'](" + src + ")") % jsvars
            if name in self.names[0] and self.names[0][name].to_js is not None:
                return self.names[0][name].to_js
            jsname = '%(globals)s[%(name)r]' % (jsvars)
        if self.options.check_defined:
            return "(typeof %(jsname)s != 'undefined' ? %(jsname)s : %(builtin)s['UnboundLocalError'](%(name)r)" % jsvars
        return jsname
dataset/ETHPy150Open anandology/pyjamas/pyjs/src/pyjs/translator_dict.py/Translator.get_jsname/original