博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
clojurescript_为什么ClojureScript在NPM上如此出色
阅读量:2524 次
发布时间:2019-05-11

本文共 5774 字,大约阅读时间需要 19 分钟。

clojurescript

by Jacek Schae

由Jacek Schae

为什么ClojureScript在NPM上如此出色 (Why ClojureScript works so well with NPM)

Every language that complies/transpiles to JavaScript wants to connect to npm to use this huge ecosystem. The master of this is, of course, ECMAScript. The second one — in my humble opinion — is ClojureScript, due to .

每种符合/编译为JavaScript的语言都希望连接到npm以使用这个庞大的生态系统。 当然,最主要的是ECMAScript。 我的第二个观点是我的拙见-由于有 ,它是 。

Disclaimer: I know there is a lot of work going on in different communities that tap into npm. By all means I’m not trying to diminish any of that by stating that CLJS (ClojureScript) is the best . I just want to give you a glance at how this works in ClojureScript.
免责声明:我知道在npm中,不同社区中正在进行很多工作。 无论如何,我并不想通过声明CLJS(ClojureScript)是最好的方法来减少任何这种情况。 我只想让您了解一下ClojureScript中的工作方式。

安装npm软件包 (Installing npm packages)

In ClojureScript, we install npm packages as we would in JavaScript. We use the standard package.json and build tool and you’ll figure out the rest.

在ClojureScript中,我们将像在JavaScript中那样安装npm软件包。 我们使用标准的package.json和构建工具,其余的您将了解。

After the installation, we have to require the package. The require statement is almost identical to import from JS. In CLJS we swap the order — first we say from where, and then what.

安装后,我们需要包装。 require语句与从JS import几乎相同。 在CLJS中,我们交换顺序-首先我们从哪里说,然后是什么。

Every ClojureScript file starts with a ns — namespace declaration. Following that we have require instead of import . And then we define a function with defn. This function will tap into our required firebase packages, and instead of using . to navigate to our method initializeApp, we use / . We make sure that when we invoke the JS method initializeApp we convert CLJS map (datastructure) to a JS Object with #js.

每个ClojureScript文件都以ns —名称空间声明开头。 接下来,我们需要而不是import。 然后我们用defn定义一个函数。 此功能将使用我们所需的firebase软件包,而不是使用。 导航到我们的方法initializeApp,我们使用/。 我们确信,当我们调用JS方法initializeApp我们把CLJS图(数据结构)的JS对象与#js

Let’s try some other npm packages to get a better feeling for the interop between npm and ClojureScript.

让我们尝试其他npm软件包,以更好地了解npm和ClojureScript之间的互操作。

React (React)

How about using React? ClojureScript has a couple of wrappers for React— the most popular one is . Here is a simple counter example with React hooks and Reagent.

如何使用React? ClojureScript有几个React的包装器-最受欢迎的是 。 这是一个带有React挂钩和Reagent的简单计数器示例。

In both examples, we first import/require React and Reagent. Then we define state in React using hooks (and in Reagent using atoms).

在这两个示例中,我们首先导入/需要React和Reagent。 然后,我们在React中使用钩子定义状态(在Reagent中使用原子定义)。

What follows is a JSX (JavaScript) and hiccup (ClojureScript) component.

接下来是一个JSX(JavaScript)和打ic(ClojureScript)组件。

This is cool, but how do we use React UI Libraries from Reagent?

这很酷,但是我们如何使用Reagent中的React UI库?

React UI库 (React UI Libraries)

One of the most popular UI libraries is . After the installation we require this library and then import our Button component as well as React. In Clojure Script we only require the Button. We don’t need to require Reagent since it’s in our ClojureScript deps. To interop with React we would use :> form and pass all of the properties that we want in a {} .

最受欢迎的UI库之一是 。 安装后,我们需要这个库,然后导入我们的Button组件以及React。 在Clojure脚本中,我们仅需要按钮。 因为它在我们的ClojureScript deps中,所以我们不需要Reagent。 要与React互操作,我们将使用:& gt; 形成并通过的所有属性,我们希望在a {}。

Redux (Redux)

How about Redux, you might ask? Well, there is a library that is build on top of Reagent, called . First designed in Dec 2014, it even pre-dates the official .

您可能会问Redux呢? 好吧,有一个库是在Reagent之上构建的,称为 。 它于2014年12月首次设计,甚至早于官方 。

By now you should have a pretty good picture as to why CLJS loves the npm ecosystem and how easy it is to interop from CLJS to JS. Maybe this is interesting to you, and you are wondering why? Why should you even try ClojureScript?

到目前为止,您应该对CLJS为什么喜欢npm生态系统以及从CLJS到JS互操作有多么容易有了一个很好的了解。 也许这对您来说很有趣,您想知道为什么吗? 为什么还要尝试ClojureScript?

为什么? (Why?)

一成不变的 (Immutable)

All ClojureScritp data structures are immutable and persistent. You don’t need to learn a new API if you want to leverage something like .

所有ClojureScritp数据结构都是不可变的和持久的。 如果您想利用东西,则无需学习新的API。

功能性 (Functional)

ClojureScript embraces Functional Programming ideas at its core. You don’t need or .

ClojureScript以函数式编程思想为核心。 你不需要或 。

简单 (Simple)

With you don’t need to spend time configuring your builds. You require what you need and the build tool will do the job.

使用您无需花费时间来配置构建。 您需要所需的内容,构建工具将完成这项工作。

简洁 (Concise)

You liability is the LoC you write . ClojureScript is one of the concise programming languages out there. Check out the last section of .

您的责任是您编写的LoC。 ClojureScript是其中一种简洁的编程语言。 查看的最后一部分。

强大 (Powerful)

ClojureScript uses for code minification and tree shaking. The same tools that Google is using to build Gmail, Google Calendar, Google Docs, and Google Maps.

ClojureScript使用进行代码最小化和摇树。 Google用于构建Gmail,Google日历,Google文档和Google地图的工具相同。

JavaScript (JavaScript)

It compiles/transpiles to JavaScript. Just as ES (EcmaScript) ReasonML, PureScript, and Elm.

它编译/编译为JavaScript。 就像ES(EcmaScript)ReasonML,PureScript和Elm。

友好 (Friendly)

The ClojureScript community is the most friendly and welcoming group of people that I have ever meet online. We mainly hang out on and .

ClojureScript社区是我网上见过的最友好,最热情的一群人。 我们主要在和上 。

全栈 (Full-Stack)

ClojureScript’s older brother, Clojure, embraces all of these ideas with Java. If you want to write your server on one of the most performant and stable platforms there is — the Java Virtuel Machine — you can do that using the same language.

ClojureScript的哥哥Clojure用Java拥抱了所有这些想法。 如果要在性能最高且最稳定的平台之一上编写服务器-Java Virtuel Machine,则可以使用相同的语言来完成。

If you like this article you should follow me on . I only write/tweet about programming and technology — mainly about ClojureScript and Clojure.

如果您喜欢这篇文章,应该在关注我。 我只写/推文有关编程和技术-主要是关于ClojureScript和Clojure。

翻译自:

clojurescript

转载地址:http://wvuzd.baihongyu.com/

你可能感兴趣的文章
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
学习进度
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
最短路径(SP)问题相关算法与模板
查看>>
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
英特尔公司将停止910GL、915GL和915PL芯片组的生产
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
正则表达式的搜索和替换
查看>>