gpt4 book ai didi

javascript - 如何使用 Kotlin 匿名类作为原生 JavaScript 函数的参数?

转载 作者:行者123 更新时间:2023-12-02 17:07:43 26 4
gpt4 key购买 nike

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

最佳答案

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

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

附注我们将来会尝试简化这个案例。

关于javascript - 如何使用 Kotlin 匿名类作为原生 JavaScript 函数的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25098376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com