scala-js-website icon indicating copy to clipboard operation
scala-js-website copied to clipboard

@ScalaJSDefined for class constructor

Open cvogt opened this issue 8 years ago • 1 comments

an example with a class with a constructor would help here: https://www.scala-js.org/doc/interoperability/sjs-defined-js-classes.html

E.g.

@ScalaJSDefined
final class Node( val id: Int, val label: String ) extends js.Object

cvogt avatar Jun 26 '16 10:06 cvogt

Or maybe this should be documented instead

@sjrd wrote

I recommend against using a class for JSON structures, as that causes your object not to be a POJO (Plain Old JavaScript Object). Instead it is an instance of a custom class. Some APIs might not like that for JSON structures. My recommendation would be:

@ScalaJSDefined
trait Node extends js.Object {
  val id: Int
  val label: String
}

Then you can create one with new Node { val id = 5; val label = "hello" } See https://www.scala-js.org/news/2016/04/30/announcing-scalajs-0.6.9/ Anonymous JS classes don't have an "own" class in JS. Instead the instances are created directly off the parent class (here, js.Object), and fields are attached to the instance.

Source: https://gitter.im/scala-js/scala-js?at=576fa492a0c12d110f84fd70

cvogt avatar Jun 26 '16 10:06 cvogt