Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reference javascript's this keyword from clojurescript?

I'm integrating some ClojureScript code with a JS library call that takes a callback function. The JS library passes data to the callback using JavsScript's "this" keyword.

I can get it to work using (js* "this"). For example:

(libraryCall (fn [] (.log console (js* "this"))))

Is there a way to get at the "this" context from ClojureScript without resorting to js*?

like image 572
Aaron Iba Avatar asked Sep 07 '12 02:09

Aaron Iba


1 Answers

Use the built-in this-as macro. It takes a name and a body, and evaluates the body with the name bound to JavaScript this.

e.g.

(libraryCall (fn [] (this-as my-this (.log js/console my-this))))

Great question... had to dig into the compiler code to find it, it's not well advertised at all.

I'll add it to the book.

like image 120
levand Avatar answered Nov 20 '22 07:11

levand