是否可以在scala中的几个文件中拆分对象定义?

时间:2014-08-28 09:49:28

标签: scala createjs scala.js

我正在编写一个Facade来在scala中使用CreateJS lib,就像这个

一样
object CreateJS
{
    sealed trait EventListener extends js.Object

    type EventHandler = js.Function

    class DisplayObject extends js.Object
    {
    }

    class Container extends DisplayObject
    {
        def addChild(item:DisplayObject):Unit = ???
        def update():Unit = ???
    }

    @JSName("createjs.Stage")
    class Stage(canvasId:String) extends Container with EventListener

    // TODO: Is enum done this way ???
    object MovieClip
    {
        @JSName("createjs.MovieClip")
        var INDEPENDENT:String = _
        @JSName("createjs.SINGLE_FRAME")
        var SINGLE_FRAME:String = _
        @JSName("createjs.SYNCHED")
        var SYNCHED:String = _
    }

    @JSName("createjs.MovieClip")
    class MovieClip(mode:js.String=MovieClip.INDEPENDENT,startPosition:js.Number=0, loop:js.Boolean=true, labels:js.Object = null) extends DisplayObject
    {

        def addEventListener(eventName:String, target:EventHandler):Unit = ???
        var timeline:TimeLine = _
        def gotoAndPlay(where:String):Unit = ???
    }

    class TimeLine extends js.Object
    {
        def addTween(tween:Tween):Unit = ???
    }

    @JSName("createjs.Shape")
    class Shape(graphics:Graphics = null) extends DisplayObject

    @JSName("createjs.Graphics")
    class Graphics extends js.Object
    {
        def beginFill(color:String):Graphics = ???
        def drawCircle(x:js.Number, y:js.Number, r:js.Number):Graphics = ???
    }

    @JSName("createjs.Ticker")
    object Ticker extends js.Object
    {
        def addEventListener(eventName:String, handler:EventHandler):Unit = ???
        def addEventListener(eventName:String, stage:Stage):Unit = ???
    }

    class Tween extends js.Object
    {
        def get(item:DisplayObject):Tween = ???
        def to(state:js.Dynamic, time:js.Number = 0):Tween = ???
    }

    @JSName("createjs.Tween")
    object Tween extends Tween


/*   Load queue */
    class Plugin extends js.Object

    @JSName("createjs.Sound")
    object Sound extends Plugin {
        def play(soundId:String):Unit = ???
    }

    @JSName("createjs.LoadQueue")
    class LoadQueue(useXHR:Boolean = true, basePath:String="", crossOrigin:String="") extends js.Object
    {
        def installPlugin(plugin:Plugin):Unit = ???

        def on(event:String, handler:EventHandler):Unit = ???

        def addEventListener(event:String, handler:EventHandler):Unit = ???

        def loadFile(params:js.Dynamic):Unit = ???

        def loadManifest(paramList:js.Array[js.Dynamic]):Unit = ???
    }


}

这个文件只是一大堆。 在真正的createJS中,这些文件分为4个文件 - 画架,补间,声音和预加载,每个扩展基础createjs对象。我是否有可能在scala中执行此文件分离,同时将所有内容保存在CreateJS命名空间中?

(也许这样的端口已经存在?)

1 个答案:

答案 0 :(得分:4)

您可以将代码分成不同的特征,每个特征负责一个连贯的方面(并收集相关的函数),每个特征在一个单独的文件中,然后让公共单例外观对象公开所有特征。

例如,特征A在文件foo中实现函数barA.scala,以及特征B包含函数oof和{{ 1}}在文件rab中,外观对象可以声明为B.scala

通过这种方式,我建议将trait声明为包私有,因为它们只用于组织代码:object Facade extends A with B