首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Kotlin匿名类作为本机JavaScript函数的参数?

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

Stack Overflow用户
提问于 2014-08-02 18:56:19
回答 1查看 1.7K关注 0票数 1

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

代码语言:javascript
运行
复制
//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 );
};

下面是我想在科特林做的事情,是否有可能以一种方式使用异常物体?我最初的想法是创建一个与传递的可能的周长相等的对象,问题是它会覆盖当前的值,而这不是我想要的。

代码语言:javascript
运行
复制
//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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-02 19:55:31

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

代码语言:javascript
运行
复制
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))

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

代码语言:javascript
运行
复制
native("THREE.PointCloudMaterial")
public class PointCloudMaterial(parameters: Any)

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

我们以后会尽量简化这个案子。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25098376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档