(ns example.core
(:import goog.Uri))
ClojureScript 專案總是會自動包含 Google Closure Library,這是一個龐大的函式庫,由 Google 建立並在其許多產品(Gmail、Docs 等)中使用。它具有用於 DOM 操作、伺服器通訊、動畫、資料結構、單元測試、富文本編輯和 UI 小部件/控制項的底層實用工具。
您可能首先需要考慮以下 ClojureScript 函式庫,它們包裝了 Google Closure Library 的某些功能。它們的原始碼也可以作為如何直接使用 Closure 的良好範例。
ClojureScript 包裝 | Closure 函式庫 |
---|---|
* 包含在 ClojureScript 的核心函式庫中
一些有用的部落格文章
要在您的 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