Using JavaScript in ClojureScript
ClojureScript allows us to use JavaScript constructs. We can call JavaScript methods and functions like any other in ClojureScript. When we called Java from Clojure we used operators such as .
dot or \
slash. Using JavaScript in ClojureScript will also require us to learn a new syntax.
While Java operates on classes a lot, in JavaScript we operate on objects. Two JavaScript constructs that we want to use on objects are:
- Methods
- Fields
In order to access a method from a JavaScript object, we place .
(a dot) followed by a method name. Accessing a field of an object is very similar. We use .-
(a dot and a hyphen) before the field name. You might wonder why accessing a function uses slightly different syntax than accessing a field. In JavaScript, an object can have a method and a field with the same name. In ClojureScript, we need a way to distinguish between a function call and a field access.
In JavaScript, the code looks as...