【问题标题】:Is there a way to determine if a type is a Scala one or a JS one in Scala.JS?有没有办法确定 Scala.JS 中的类型是 Scala 类型还是 JS 类型?
【发布时间】:2015-10-08 18:48:31
【问题描述】:

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

对象栏 案例类 Foo(x: Int) def isAScalaType(x: Any) : Boolean = {/* 这个函数的实现是什么 */} isAScalaType(js.Dynamic.literal(Hello="World")) // 返回 false isAScalaType(Foo(1)) // 返回 true isAScalaType(Bar) // 返回真 //包装器如 Int / Double / String 等也应该返回 true: isAScalaType("abc") // 真 isAScalaType(123) // 真 isAScalaType(1.0) // 真

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

【问题讨论】:

    标签: scala.js


    【解决方案1】:

    建议的方法是在值上调用 .getClass 并查看它是否为空

    例如

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

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      相关资源
      最近更新 更多