"用“;在Scala中从JavaScript构建
"with" construct from JavaScript in Scala
Scala是否有类似于JavaScript的with
的东西,或者它是否可以以某种方式实现(例如库、implicits或宏(?
JS中的with
示例:
function p(){
document.write("I am " + this.constructor.name + ".<br>");
}
function o1(){
this.p = p;
}
var i1 = new o1();
i1.p();
p();
with(i1){ p(); }
输出:
I am o1.
I am Window.
I am o1.
我正在寻找的示例:
class A { def x { ... } }
val a = new A
with(a){ x }
我唯一想到的是使用匿名函数,但这只是一个名称替换,仅此而已(而且非常冗长(。
( (aa:A) => aa.x )(a)
import a._
// code below can now use a's methods without referencing it
若要限制导入范围,请使用大括号:
{
import a._
// namespace madness
}
// everything's back to normal here
REPL示例:
scala> val i = ""
i: java.lang.String = ""
scala> import i._
import i._
scala> length
res0: Int = 0
您也可以执行有限的导入:
import a.{thingImGonnaUse, anotherImportantThing}
甚至将其重命名为
import a.{thingImGonnaUse => tigu, anotherImportantThing => ait}
这比只做val ait = a.anotherImportantThing _
更强大,因为它仍然允许您使用重载版本:
scala> val i = "0123456"
i: java.lang.String = 0123456
scala> import i.{substring => x}
import i.{substring=>x}
scala> x(3)
res1: java.lang.String = 3456
scala> x(2,3)
res2: java.lang.String = 2
scala> val y = i.substring _
<console>:15: error: ambiguous reference to overloaded definition,
both method substring in class String of type (x$1: Int, x$2: Int)java.lang.String
and method substring in class String of type (x$1: Int)java.lang.String
match expected type ?
val y = i.substring _
^
相关文章:
- 如何在asp.net中从javascript警报到文本框中获取值
- 如何在 C# 中从 JavaScript 对象获取值
- 在 SharePoint 中从 JavaScript 调用 C# 代码
- 有没有办法在Webview中从JavaScript调用Java
- 如何在 asp.net 中从 javascript 调用服务器端代码
- 如何在 Python 中从 JavaScript CryptoJS.AES.encrypt(password, pass
- 如何在asp.net中从javascript调用codeehind函数
- 如何在MVC3中从javascript调用Controller方法
- 在CRM 2011中从JavaScript执行工作流
- 在 PHP MVC 中从 Javascript 调用操作方法
- 如何在 Opera 中从 Javascript 访问 8 位二进制数据
- 如何在 C# 中从 JavaScript 规范化 JSON
- ExtJs4 在IE中从javascript生成Excel文件得到我:“传递给系统调用的数据区域太小”
- 无法在我的 Android 应用程序中从 Javascript 调用我的 Java 类
- 在 ASP.NET Web 窗体中从 javascript/客户端设置属性背后的代码
- 在IE中从Javascript调用Silverlight代码
- 在 WebSQL 中从 javascript 将 currentDate 插入 WebSQL 中的问题
- 在Internet Explorer中从javascript打印方法设置pdf的文件名不起作用
- 在play框架中从javascript调用java函数
- 如何在PhoneGap Windows Phone中从JavaScript调用C#代码