簡體   English   中英

如何使用Kotlin匿名類作為本機JavaScript函數的參數?

[英]How to use Kotlin anonymous classes as arguments for native JavaScript Functions?

我正在為此 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);

類型安全的解決方案可能如下所示:

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 })

附言:我們將在以后嘗試簡化這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM