我有一个代码库,它在嵌套表单中大量使用get和get-in。我也希望能够使用原生javascript对象,而不需要重写代码。
js> cljs.user.o = {foo: 42} // in js console
cljs.user> (get o "foo") ; => 42 ; in cljs console因为我只查询表单,而不修改它们,所以我认为实现get ( get-in所依赖的)就足够了。这是我的尝试,
(extend-protocol ILookup
js/Object
(-lookup [m k] (aget m k))
(-lookup [m k not-found (or (aget m k) not-found)))它似乎起作用了,但它以一种奇怪的方式破坏了很多东西。
发布于 2013-06-04 03:17:33
你正在修改对象原型,你不想这样做,下面是更好的方法:
(extend-protocol ILookup
object
(-lookup [m k] (aget m k))
(-lookup [m k not-found] (or (aget m k) not-found)))https://stackoverflow.com/questions/16894465
复制相似问题