user2975535 user2975535 - 2 months ago 12x
Scala Question

How to create global Javascript variable in Scala js?

I need to create global JS variable in my Scala js.
How this can be done?


Via Facade Type

Make an object that extends js.GlobalScope:

object MyGlobal extends js.GlobalScope {
  var globalVar: String

Then, just assign to it:

MyGlobal.globalVar = "foo"

Via Dynamic interface = "foo"


Both of these methods need code to be executed before the variable is set to anything. There is currently no way to directly export a top-level value in Scala.js without executing code (see #1381).