user2975535 user2975535 - 1 year ago 93
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 Source

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