gpt4 book ai didi

Clojurescript this-as 宏指向全局对象

转载 作者:行者123 更新时间:2023-12-03 13:37:25 25 4
gpt4 key购买 nike

在 clojurescrtipt 中编写 ReactJS 教程时,我发现 this-as 宏编译为

(function(){var t = this; return t;}

它总是指向 react 类中的窗口。有时我可以通过 js* this 解决这个问题,但不能在 letmap 内部解决,因为它们也被编译为函数。

我如何在let表单中访问react js this

fiddle 情况:http://jsfiddle.net/VkebS/57/

还有一段教程仅供引用:

(def comment-list 
(React/createClass
#js{:render
(fn [] (dom/div #js {:className "commentList"}
(let [d (this-as t (.. t -props -data))]
(map #(commnt #js {:author (:author %)} (:text %)) d))))}))

PS:我可以使用 native 数组作为数据和 native map 功能

(def comment-list 
(React/createClass
#js{:render
(fn [] (dom/div #js {:className "commentList"}
(.map (.. (js* "this") -props -data) #(commnt #js {:author (:author %)} (:text %)))))}))

这可行,但是......

最佳答案

this-as 如果您在 render 函数开始时使用它,则它会起作用:

(def commnt
(React/createClass
#js {:render
(fn []
(this-as this
(dom/div #js {:className "comment"}
(dom/h2 #js {:className "commentAuthor"}
(.. this -props -author))
(dom/span #js {:dangerouslySetInnerHTML
#js{:__html
(.makeHtml converter (.. (js* "this") -props -children toString))}}))))}))

另请参阅:https://github.com/swannodette/om/blob/master/src/om/dom.cljs#L34

关于Clojurescript this-as 宏指向全局对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20597568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com