Skip to content

Instantly share code, notes, and snippets.

@mcdoyaji
Last active November 16, 2015 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcdoyaji/d78786d51bdf36f14137 to your computer and use it in GitHub Desktop.
Save mcdoyaji/d78786d51bdf36f14137 to your computer and use it in GitHub Desktop.
// -----------------------------------------------
// scalaJS 로 스칼라 오브젝트, 클래스, 메소드 등을
// JS 로 내보내기 예제
// ------------------------------------------------
import js.annotation.JSExport
import js.annotation.JSExportNamed
import js.annotation.JSExportAll
import scala.annotation.meta.field
import js.annotation.JSExportDescendentObjects
// ScalaJSExample().main() 으로 호출
// 자바스크립트에서는 함수로 내보내진다.
// 외부에서 호출되려면 최소 하나의
// 클래스나 오브젝트와 메소드가 내보내져야 함.
@JSExport("app")
object ScalaJSExample extends js.JSApp{
@JSExportNamed
def bar(first:String, last:String) : Unit = println(""+ first + last)
@JSExportNamed
def foo(x: Int, y: Int = 2, z: Int = 3) = x+y+z
@JSExport
def main(): Unit = {
println("hello");
}
}
@JSExport
class Mine(val x: Int){
@JSExport
def add(y:Int) : Int = x+y
@JSExport
def add(y:Int, z:Int) : Int = x+y+z
}
@JSExport
class Point(ex: Double, why: Double) {
@JSExport
val x: Double = ex
@JSExport
var y: Double = why
@JSExport
def abs: Double = Math.sqrt(x*x + y*y)
@JSExport
def sum: Double = x + y
@JSExport
def sum_=(v: Double): Unit = y = v - x
@JSExport
def name_=(v: Double):Unit = y = v - x
}
@JSExport
class Loint(
@(JSExport @field) val x: Double,
@(JSExport @field) val y: Double){
@JSExport
def loAdd = x+y
}
// Also applies to case classes
@JSExport
case class Voint(
@(JSExport @field) x: Double,
@(JSExport @field) y: Double){
@JSExport
def voAdd = x+y
}
@JSExportDescendentObjects
trait Test {
@JSExport
def test(param: String):Unit
}
// automatically exported as foo.test.Test1
object Test1 extends Test {
// exported through inheritance
def test(param: String) = {
println(param)
}
}
class Parent {
def pDef(x: Int, y: Int): Int = x * y
}
@JSExport
@JSExportAll
class Child(val a: Int) extends Parent {
override def pDef(x: Int, y: Int): Int = x * y
def cDef(x: Int, y: Int): Int = x + y
}
// @JSExport 와 @JSExportAll 을 함께 써야 하는지 따로 써야 하는지에 관한 답변
// https://github.com/scala-js/scala-js/issues/1476
//
// @JSExport exports the object itself (or the class' constructor, if it is a class)
// @JSExportAll exports all its members, but not the object/constructor itself.
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment