scala.js - 有没有办法在 Scala.JS 中确定一种类型是 Scala 类型还是 JS 类型?

标签 scala.js

在 Scala JS 中,有没有一种方法可以确定一个值是 Scala 值还是原生 js 值?

object Bar
case class Foo(x: Int)

def isAScalaType(x: Any) : Boolean = {/* What is the implementation of this function */}

isAScalaType(js.Dynamic.literal(Hello="World")) // returns false

isAScalaType(Foo(1)) // returns true
isAScalaType(Bar) // returns true

//Wrappers such as Int / Double / String etc should also return true:
isAScalaType("abc") // true
isAScalaType(123) // true
isAScalaType(1.0) // true

(这个问题最初是在gitter聊天室问的)

最佳答案

建议的方法是对值调用 .getClass 并查看它是否为 null

例如

def isAScalaType(x: Any) : Boolean = x.getClass != null

关于scala.js - 有没有办法在 Scala.JS 中确定一种类型是 Scala 类型还是 JS 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31503079/

相关文章:

ScalaPB 与 Scala.js 和 Scala(jvm) - 存在链接错误

scala - 如何在 scala.js 中使用 scala.sys.process

node.js - 如何在 scala.js 中提供和使用 require.js 模块(和扩展类)

scala - scala-js 的无形端口 : create artifact with few external dependencies

node.js - 如何从 scala.js 调用 nodejs 模块?

scala.js - 对 scala.js 中的日期使用感到困惑

Scala.js:如何将 Seq[T] 转换为 js.Array[T]

scala.js - 如何导出共享案例类的属性

multithreading - 如何在 future 中包装 Web Worker 响应消息?