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

不问归期 提交于 2020-01-22 17:51:12

问题


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*?


回答1:


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.



来源:https://stackoverflow.com/questions/12310950/how-do-you-reference-javascripts-this-keyword-from-clojurescript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!