Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to not pollute global namespace? #877

Open
CreatCodeBuild opened this issue Nov 2, 2018 · 3 comments
Open

Is there a way to not pollute global namespace? #877

CreatCodeBuild opened this issue Nov 2, 2018 · 3 comments

Comments

@CreatCodeBuild
Copy link

Is it possible to not compile code into global but to an object the user can supply?

I am imagining something like

// xxx.js
// Instead of doing
(function() { // ... GopherJS Code })(this)
// Do
(function() { // ... GopherJS Code })(myObject)

So that the importer/user can do

import myObject from "xxx"
@dmitshur
Copy link
Member

dmitshur commented Nov 4, 2018

How much global namespace pollution is there? The generated JavaScript code is contained in a closure, and this parameter is needed for js.Global to access the right thing.

You can do things like js.Global.Set("foo", ...) to export something from Go code to other JavaScript code, which is described at https://github.com/gopherjs/gopherjs#providing-library-functions-for-use-in-other-javascript-code.

@flimzy
Copy link
Member

flimzy commented Apr 9, 2021

I've often wished for this, as well. Not becuase pollution is a problem, per se, but because there's no way to know if my global name is used by some other package, so I end up creating global variable names longer than any reasonable person would ever use, just to reduce the chance of collision.

@CreatCodeBuild
Copy link
Author

@flimzy Actually that's what I meant by "polluting the global space". Too bad that I haven't get a chance do any GO + JS project for real. So I don't have much input here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants