使用JavaScript Clojurescript互操作性 [英] Clojurescript interoperability with JavaScript

查看:118
本文介绍了使用JavaScript Clojurescript互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要使用角度构建的应用程序。现在,我想转为该项目Clojurescript。 Clojurescript有非常漂亮的互操作使用JavaScript,因为我们都知道,但是它周围做其他的方式是否可行?

I have built an application using mostly Angular. Now I'd like to transition the project to Clojurescript. Clojurescript has very nice interop with JavaScript as we all know but is it feasible to do the other way around?

如何能常规的JavaScript /角code点击进入的JavaScript Clojurescript产生的?

How can regular JavaScript /Angular code tap into the JavaScript generated from Clojurescript ?

这个梦将是clojurescript编写新的功能,并让它们并排与传统code携手并肩。

The dream would be to write new features in clojurescript and have them work side by side with the legacy code.

这方面的任何提示或技巧是受欢迎的。

Any tips or tricks regarding this are welcome.

推荐答案

Clojurescript瓦尔,功能和deftypes /记录是正常的JS瓦尔,功能和构造函数/对象,所以你可以去 cljs .core.abc()在JavaScript并调用clojurescript没有任何问题。

Clojurescript vars, functions, and deftypes/records are normal JS vars, functions, and constructor functions/objects, so you can go cljs.core.abc() in your javascript and invoke clojurescript without problems.

注意事项有:


  1. 名称需要改写。 Clojurescript名称允许范围更广的JavaScript比人物,这么多的功能名称将被改写的。例如。 cljs.core /< 变成 cljs.core._LT _

  2. 宏。宏只存在于clojurescript编译时间,所以你不能从JavaScript中使用一个CLJS宏。

  3. 高级编辑。 Clojurescript核心是非常大的,它依赖于谷歌关闭编译器为死区code消除,这是只有在高级编译可用。采用先进的编译安全是纯clojurescript code,很容易,但在JavaScript更难code 。 (最大的危险是混合字符串和符号属性访问。)

  1. Name munging. Clojurescript names allow a wider range of characters than javascript, so many function names will be munged. E.g. cljs.core/< becomes cljs.core._LT_.
  2. Macros. Macros only exist at clojurescript compile-time, so you can't use a CLJS macro from javascript.
  3. Advanced compilation. Clojurescript core is very large and it relies on the Google Closure compiler for dead-code elimination, which is only available in advanced compilation. Using advanced compilation safely is very easy in pure clojurescript code, but harder in javascript code. (The biggest danger is mixing string and symbol property access.)

您有多种选择:


  • CLJS嵌入传统code作为一个库。你的程序入口点是CLJS但使用旧版code像外部库。旧版code提供了在那里CLJS调用您的遗留code一个外部。传统code只要求CLJS功能,这是使用 ^明确出口:出口(如果你定义的名称)或类似的 goog.exportSymbol 。 CLJS和传统code被部署在其中分别建立单独的JS文件。

  • CLJS直接嵌入传统code。程序入口点是CLJS,但遗留code是编译到同一项目的其他JS。 CLJS和传统code被部署在由clojurescript编译器构建一个单一的JS文件。要使用先进的编译,你的遗产code必须是安全的具有先进的编译使用,并构造用于与谷歌Closure编译器使用(goog.provides /需要,类型标注等)。角度可能要保持分离,但有一个角的extern可用

  • 传统code调用CLJS code作为一个库。这是pretty一样一样的CLJS嵌入传统code作为一个库,除了你的切入点是传统code和它调用出口cljs功能。

  • 传统code通过像库使用一些CLJS功能。在这里,您不使用CLJS但直接暴露clojurescript正常JS的一些功能的JS库。它仍然是一个独立的JS库,你永远不会写任何clojurescript。

  • CLJS embeds legacy code as a library. Your program entry point is CLJS but uses your legacy code like an external library. Your legacy code provides an extern for where CLJS calls your legacy code. Legacy code only calls CLJS functions which are explicitly exported using ^:export (if you define the name) or something like goog.exportSymbol. CLJS and legacy code are deployed in separate JS files which are built separately.
  • CLJS embeds legacy code directly. Program entry point is CLJS, but your legacy code is other js compiled into the same project. CLJS and legacy code are deployed in a single JS file built by the clojurescript compiler. To use advanced compilation, your legacy code must be safe to use with advanced compilation and structured for use with the Google Closure compiler (goog.provides/requires, type annotations, etc). Angular will probably have to remain separate, but there is an angular extern available.
  • Legacy code calls CLJS code as a library. This is pretty much the same as "CLJS embeds legacy code as a library," except your entry point is legacy code and it calls exported cljs functions.
  • Legacy code uses some CLJS features via library like mori. Here you are not using CLJS directly but a JS library that exposes some features of clojurescript as normal JS. It remains a separate js library, and you never write any clojurescript.

这篇关于使用JavaScript Clojurescript互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆