2015-07-19 50 views
2

在斯卡拉JS有没有一种方法来确定一个值是Scala one还是native js one?有没有一种方法可以确定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 

(这个问题最初是在小胶质聊天室问)

回答

2

建议的方法是调用值.getClass,看看它是否是空

例如

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

相关问题