开发者

How does the "jimport" function from "etherpad" work?

In etherpad's source I've seen code like:

j开发者_运维百科import(java.util.Date, "JDate");

But it doesn't seem like some kind of javascript standard.

How is it done?


Looks like you're looking at the etherpad source. There they defined a helper function named jimport.

As mentioned, etherpad uses Rhino as its Javascript engine, which have Java available at his fingertips. 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.

This function receives the package name, and the global scope to which it'll inject the package to, and given a.b.c its essentially calling globalscope[c] = a.b.c, in order to import this Java package.


The ability to load Java libraries in JavaScript is highly-dependent on which server-side JavaScript framework you're using.

Loading Java is definitely supported by Rhino. From the docs:

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


Another way to use Java classes from JavaScript:

<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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜