未加星标

JS Changes Fast; CLJS Makes It Manageable

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

I wrote my first javascript somewhere around 15 years ago and switched almost exclusively to ClojureScript several years ago. JavaScript as a language has changed so much that, if you drop me in any modern js codebase, it’s probably 50/50 that I could actually read it without referencing google.

JavaScript is constantly changing with new syntax, new data structures, new features, etc. And for the most part, using ClojureScript just lets me ignore the constant change and focus on solving problems.

Changes in JavaScript, Handled by ClojureScript Pipeline operator

When the pipeline operator was first proposed, developers seemed very excited about it. I was confused at the excitement because it’s just the threader macro in ClojureScript.

Example from How to try the JavaScript Pipeline Operator Today

//js const double = (n) => n * 2; const increment = (n) => n + 1; // without pipeline operator double(increment(5)); // 12 // with pipeline operator const result = 5 |> increment |> double result // 12

;;cljs (defn double [n] (* n 2)) (defn increment [n] (+ n 1)) ;;without threader (double (increment 5)) ;; 12 ;;with threader (-> 5 increment double) ;; also have ->>, as->, some->, and cond-> Optional Chaining Operator

The optional chaining operator cleans up all checks that your properties exist when navigating an object. In ClojureScript, we would use a map and just use get-in . And if we have an object, we use Google Closure’s goog.object .

Example from ESNext: JavaScript “Optional Chaining Operator”

//js const message = { body: { user: { firstName: 'Bramus', lastName: 'Van Damme', }, }, }; //have to make sure all properties exist const firstName = (message.body && message.body.user && message.body.user.firstName) || 'Stranger'; //don't need all the intermediate checks const firstName = message.body?.user?.firstName || 'Stranger';

;;cljs with a map (def message {:body {:user {:first-name "Bramus" :last-name "Van Damme"}}}) (let [first-name (get-in message [:body :user :first-name] "Stranger")] ...) ;;cljs with an object (def message #js {:body #js {:user #js {:first-name "Bramus" :last-name "Van Damme"}}}) (let [first-name (or (goog.object/getValueByKeys message #js ["body" "user" "first-name"]) "Stranger")] ...) Object Manipulation

There are plenty of new ways of defining, destructuring, and working with objects. Most of these are irrelevant to ClojureScript because we use maps instead of objects and have a powerful API to work with data. The new Object.entries and Object.values are just keys and vals when working with maps. JavaScript added destructuring and default values which are just part of ClojureScript’s core language.

Generators and Async/Await

I can see why JavaScript developers are excited about Generators and Async/Await, but I’ve yet to see anything that couldn’t be implemented with core.async . It wouldn’t surprise me if ClojureScript could use these internally for performance gains, but as a ClojureScript user it’s not a new feature that unlocks new value.

Regular Expressions

Regular expressions are one exception where ClojureScript developers need to keep up with JavaScript, because ClojureScript regular expressions compile to native JavaScript regular expressions. There are several proposals for new features with JavaScript regular expressions that will be visible in ClojureScript.

In general, ClojureScript makes it so that I just don’t think much about JavaScript as a language. ClojureScript deserves a lot of praise for the stability it provides given the frequent changes of the language it’s built on. For the last several years, ClojureScript developers have been free to spend time learning a consistent API without needing to track frequent language and syntax changes.

Find this post useful? Share the love!

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:JS Changes Fast; CLJS Makes It Manageable
本站链接:https://www.codesec.net/view/628600.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(36)