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

Document extending JavaScript interfaces

Open ohde opened this issue 9 years ago • 5 comments

I believe after https://github.com/scala-js/scala-js-website/blob/master/doc/calling-javascript.md#defining-javascript-interfaces-with-traits it would be beneficial to show extending a Javascript interface. That way users can easily see how to enrich their JavaScript APIs with minimal work. I have provided an example below. However, I am sure a more useful example can be provided.

trait Window extends js.Object { 
  def alert(message: String): Unit = js.native
} 
object Window {
  implicit class RichWindow(val self: Window) extends AnyVal {
    def help: Unit = self.alert("help")
  }
}

ohde avatar Mar 18 '15 12:03 ohde

Yes, it's probably worth documenting it. Although it's a fairly common general Scala idiom.

sjrd avatar Mar 18 '15 12:03 sjrd

Yes, I agree with you. But it might be worth at least putting in an aside for people experimenting with scalajs who are not that familiar with Scala.

ohde avatar Mar 18 '15 12:03 ohde

This is now documented in http://www.scala-js.org/doc/interoperability/facade-types.html

ochrons avatar Nov 27 '15 14:11 ochrons

Is it? Searching implicit class does not yield any result.

sjrd avatar Nov 27 '15 14:11 sjrd

Ah, you are right, didn't look closely enough :)

ochrons avatar Nov 27 '15 14:11 ochrons