在 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/