user2975535 user2975535 - 3 months ago 22
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?

Answer

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

js.Dynamic.global.globalVar = "foo"

Caveat

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).

Comments