简体   繁体   English

“etherpad”中的“jimport”功能如何工作?

[英]How does the “jimport” function from “etherpad” work?

In etherpad's source I've seen code like: 在etherpad的源代码中,我看过如下代码:

jimport(java.util.Date, "JDate");

But it doesn't seem like some kind of javascript standard. 但它似乎不是某种javascript标准。

How is it done? 怎么做?

Looks like you're looking at the etherpad source. 看起来你正在看以太网源。 There they defined a helper function named jimport . 他们在那里定义了一个名为jimport辅助函数

As mentioned, etherpad uses Rhino as its Javascript engine, which have Java available at his fingertips. 如上所述,etherpad使用Rhino作为其Javascript引擎,它可以在指尖轻松获得Java。 So it is enough to make 所以它就足够了

file = java.io.File

in order to "import" it. 为了“导入”它。

This function uses the helper function _jimportSinglePackage , to inject the Java package into the local Javascript scope. 此函数使用辅助函数_jimportSinglePackage将Java包注入本地Javascript范围。

This function receives the package name, and the global scope to which it'll inject the package to, and given abc its essentially calling globalscope[c] = abc , in order to import this Java package. 这个函数接收包名称,以及它将包注入的全局范围,并给出abc它本质上调用globalscope[c] = abc ,以便import这个Java包。

The ability to load Java libraries in JavaScript is highly-dependent on which server-side JavaScript framework you're using. 在JavaScript中加载Java库的能力在很大程度上取决于您正在使用的服务器端JavaScript框架。

Loading Java is definitely supported by Rhino . Rhino肯定支持加载Java。 From the docs: 来自文档:

js> importPackage(java.io)
js> File
[JavaClass java.io.File]

Another way to use Java classes from JavaScript: 从JavaScript使用Java类的另一种方法:

<html>
<body>
<script type="text/javascript">
document.write(new java.util.Date());
</script>
</body>
</html>

See Call a Java method from Javascript for more details. 有关更多详细信息,请参阅从Javascript调用Java方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM