special form | since v0.0-927 | clojure.core/. | Edit |
(. .instanceMethod instance args*)
(. .-instanceField instance)
For interop, the .
special form allows access to member properties of the
given JavaScript object o
.
If the second operand is a symbol preceded with a hyphen as in -p
, the
expression will result in the value of the property named p
.
If the second operand is a symbol that is not preceded with a hyphen as in m
,
the expression will evaluate to a call of the method named m
. Any additional
operands will be passed as arguments to the method.
The preferred, idiomatic way to access members of a JavaScript object is to use the following sugar:
Sugar | Expands To |
---|---|
(.-p o) |
(. o -p) |
(.m o) |
(. o m) |
(.m o 1 2) |
(. o m 1 2) |
We can access the JavaScript properties of a string:
// JavaScript
var m = "Hello World";
m.length;
//=> 11
;; ClojureScript
(def m "Hello World")
(.-length m)
;;=> 11
We can also call member functions on the string:
// JavaScript
m.toUpperCase();
//=> "HELLO WORLD"
m.replace("H", "");
//=> "ello World";
;; ClojureScript
(.toUpperCase m)
;;=> "HELLO WORLD"
(.replace m "H" "")
;;=> "ello World"
Create a JavaScript object o
:
(def o #js {:foo "bar"})
You can get the value at property "foo"
with any of the following:
(. o -foo)
;;=> "bar"
(.-foo o)
;;=> "bar"
(aget o "foo")
;;=> "bar"
The instance member form works for methods and fields. They all expand into calls to the dot operator at macroexpansion time.
(defmethod parse '.
[_ env [_ target & [field & member+] :as form] _ _]
(disallowing-recur (analyze-dot env target field member+ form)))