user79074
user79074

Reputation: 5270

Representing js.Dynamic in an object or class

Say I want to produce the following javascript:

var myObj = { x: 'a', y: 'b' }

I can do this by callaing js.Dynamic.literal:

val myObj = js.Dynamic.literal(x = "a", y = "b")

But I can also represent this kind of info in a class:

@js.native
object MyObj extends js.Object {
    val x = "a"
    val y = "b"
}

val myObj = MyObj

But since I upgraded to 6.21 this causes the compiler warnings:

Members of traits, classes and objects extending js.Any may only contain members that call js.native. This will be enforced in 1.0.

val x = "a"

...

Is there I way I can continue with this approach going forward?

Upvotes: 0

Views: 42

Answers (1)

pme
pme

Reputation: 14803

I tried to redo it on Scalafiddle.

The exception looks a bit different, but doing as suggested the final solution without warnings looks like this:

import scala.scalajs.js.annotation.ScalaJSDefined

@ScalaJSDefined
object MyObj extends js.Object {
    val x = "a"
    val y = "b"
}

println(MyObj.x + MyObj.y)

Here the Scalafiddle.

Upvotes: 0

Related Questions