-
Notifications
You must be signed in to change notification settings - Fork 790
Google Closure Library
DEPRECATION NOTICE: Please do not edit this wiki. Instead submit pull requests to https://github.com/clojure/clojurescript-site
The up to date version of this page can be found at https://clojurescript.org/reference/google-closure-library
ClojureScript projects always auto-include the Google Closure Library, a massive library built and used by Google on many of their products (Gmail, Docs, etc). It has low-level utilities for DOM manipulation, server communication, animation, data structures, unit testing, rich-text editing, and UI widgets/controls.
- Google Closure Library API Reference
- Closure Cheatsheet - abridged API with usage examples
You may first want to consider the following ClojureScript libraries which wrap some functionality from Google Closure Library. Their source code also serves as good examples of how to use Closure directly.
ClojureScript wrapper | Closure Libraries |
---|---|
cljs-time | goog.date |
cljs-http | goog.net, goog.uri |
cljs-ajax | goog.net, goog.uri, goog.json |
cuerdas | goog.string |
* included in ClojureScript's core library
Some helpful blog posts:
- ClojureScript Made Easy - Don't forget about Google Closure
- ClojureScript Made Easy - When do I use :require vs :import?
To use Google Closure in your ClojureScript code, the rule is to use:
-
:import
for Closure classes (which are also namespaces, likegoog.Uri
) and enums -
:require
for everything else
This is only for when you would like to refer directly to a class which is also a namespace, otherwise just use :require
in your ns
form or use the require
REPL helper.
(ns example.core
(:import goog.Uri))
;; in REPL
(import 'goog.Uri)
(Uri. "http://example.com")
;;=> #<http://example.com>
(ns example.core
(:import [goog.events EventType]))
;; in REPL
(import '[goog.events EventType])
EventType.CLICK
;;=> "click"
(ns example.core
(:require [goog.math :as math]))
;; in REPL
(require '[goog.math :as math])
(math/clamp -1 0 5)
;;=> 0
Sometimes symbols are __not auto-included__ when requiring their parent namespace. This happens when those symbols are in their own file and require specific inclusion |
(ns example.core
(:require
[goog.string :as gstring]
goog.string.format))
;; in REPL
(require '[goog.string :as gstring])
(require 'goog.string.format)
(goog.string.format "%05d" 123)
;;=> 00123
;; or use the alias
(gstring/format "%05d" 123)
;;=> 00123
You can look for cljs
files on Github that use goog.dom
with the following search:
Search GitHub: "[goog.dom extension:cljs](https://github.com/search?utf8=%E2%9C%93&q=goog.dom+extension%3Acljs&type=Code&ref=searchresults)"
Or you can search Google Closure Library on Github for keywords pertaining to a function it might have:
Search Closure Library on Github: "[hours minutes](https://github.com/google/closure-library/search?utf8=%E2%9C%93&q=hours+minutes)"
- Rationale
- Quick Start
- Differences from Clojure
- [Usage of Google Closure](Google Closure)