ClojureScript

Google Closure Library

ClojureScript 專案總是會自動包含 Google Closure Library,這是一個龐大的函式庫,由 Google 建立並在其許多產品(Gmail、Docs 等)中使用。它具有用於 DOM 操作、伺服器通訊、動畫、資料結構、單元測試、富文本編輯和 UI 小部件/控制項的底層實用工具。

先嘗試使用包裝函式庫!

您可能首先需要考慮以下 ClojureScript 函式庫,它們包裝了 Google Closure Library 的某些功能。它們的原始碼也可以作為如何直接使用 Closure 的良好範例。

ClojureScript 包裝 Closure 函式庫

cljs-time

goog.date

cljs-http

goog.net.XhrIo, goog.uri

cljs-ajax

goog.net.XhrIo, goog.uri, goog.json

cuerdas

goog.string

glögi

goog.log

* 包含在 ClojureScript 的核心函式庫中

直接使用 Google Closure

一些有用的部落格文章

要在您的 ClojureScript 程式碼中使用 Google Closure,規則是使用

  • :import 用於 Closure 類別(它們也是命名空間,例如 goog.Uri)和列舉

  • :require 用於其他所有項目

匯入類別

這僅適用於當您想要直接引用是命名空間的類別時,否則只需在您的 ns 表單中使用 :require,或使用 require REPL 輔助程式。

(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

當符號位於自己的檔案中且需要特定包含時,它們在要求父命名空間時,有時不會自動包含

(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

搜尋範例

您可以在 Github 上搜尋使用 goog.domcljs 檔案,使用以下搜尋條件

Search GitHub: "goog.dom extension:cljs"

或者您可以在 Github 上搜尋 Google Closure Library,搜尋與它可能具有的函式相關的關鍵字

Search Closure Library on Github: "hours minutes"