如何使用Kotlin匿名类作为本机JavaScript函数的参数?

时间:2014-08-02 18:56:20

标签: javascript interop kotlin

我正在为this ThreeJS类设置互操作层,并且该类的构造函数接受一个用于设置属性的对象。

//PointCloudMaterial.js    
THREE.PointCloudMaterial = function ( parameters ) {
    THREE.Material.call( this );
    this.color = new THREE.Color( 0xffffff );
    this.map = null;
    this.size = 1;
    this.sizeAttenuation = true;
    this.vertexColors = THREE.NoColors;
    this.fog = true;
    this.setValues( parameters );
};

以下是我希望能够在Kotlin中做到的事情,是否有可能在时尚中使用异常物体?我原本打算创建一个等同于可能的传递周长的对象,问题是它会覆盖当前值,这不是我想要的。

//Interop Layer
native("THREE.PointCloudMaterial")
public class PointCloudMaterial(parameters: object) { } //This doesn't compile "Type Expected"

//Example usage
var sizeObject = object {
     var size: Double = size
}
PointCloudMaterial(sizeObject);

1 个答案:

答案 0 :(得分:2)

类型安全解决方案可能如下所示:

native 
val <T> undefined: T = noImpl

class PointCloudMaterialParameters (
   val color: Int = undefined,
   val opacity: Double = undefined,
   //val map: THREE.Texture = undefined,
   val size: Double = undefined,
   //val blending: THREE.NormalBlending = undefined,
   val depthTest: Boolean = undefined,
   val depthWrite: Boolean = undefined,
   val vertexColors: Boolean = undefined,
   val fog: Boolean = undefined
)

fun main(args : Array<String>) {
  println(PointCloudMaterialParameters(size = 2.0))
}

native("THREE.PointCloudMaterial")
public class PointCloudMaterial(parameters: PointCloudMaterialParameters)

//Example usage
PointCloudMaterial(PointCloudMaterialParameters(size = 2.0))

另一种较短但不安全的解决方案是:

native("THREE.PointCloudMaterial")
public class PointCloudMaterial(parameters: Any)

//Example usage
PointCloudMaterial(object { val size = 2.0 })

P.S。我们将在未来尝试简化此案例。