Javascript ClojureScript在高级优化中对同一类代码的名称进行不同的修改
这是Chrome扩展的一段代码 我从ClojureScript开始,所以这可能是一个很小的问题,虽然我已经找到了解决方案,但我仍然不明白为什么会出现这个问题 这是一段代码,它磨碎了我的齿轮:Javascript ClojureScript在高级优化中对同一类代码的名称进行不同的修改,javascript,google-chrome-extension,clojurescript,Javascript,Google Chrome Extension,Clojurescript,这是Chrome扩展的一段代码 我从ClojureScript开始,所以这可能是一个很小的问题,虽然我已经找到了解决方案,但我仍然不明白为什么会出现这个问题 这是一段代码,它磨碎了我的齿轮: (defn print-and-save-selection [info tab] (let [selection-text (.-selectionText info)] (println info) ;; => #js { bunch of properties, "selecti
(defn print-and-save-selection
[info tab]
(let [selection-text (.-selectionText info)]
(println info) ;; => #js { bunch of properties, "selectionText" being one }
(.get js/chrome.storage.sync
(clj->js {:history nil :historyItems []})
(fn [items]
(println items) ;; => #js {:history nil :historyItems #js []}
(let [history-items (.-historyItems items)
updated-history-items (.concat history-items selection-text)]
(println updated-history-items) ;; => correct list in :whitespace mode
;; save stuff using other chrome.storage function
我已正确设置了外部程序,并且此代码在:whitespace
优化下可以正常工作,但在:advanced
下不能正常工作。对于后者,它为我提供了一个无法读取未定义的属性“concat”。换句话说,(.-historyItems)
无法在项中找到适当的元素。如果我将其更改为(aget items“historyItems”)
,即使在:advanced
中也可以使用
我不明白的是,当(-selectionText-info)
(第三行)和(-historyItems)
都在访问JavaScript对象的属性时,为什么适用于(-selectionText-info)
(第三行)的功能不适用于(-historyItems)
。这可能与此代码的嵌套级别有关
为完整起见,生成的代码如下所示:
:无aget的高级
var a = b.selectionText;
...
// a few nested returns and functions ...
var h = g.Ab, c = h.concat(a); // => error mentioned above
var a = b.selectionText;
...
// a few nested returns and functions ...
var h = g.historyItems, c = h.concat(a);
:使用aget进行升级
var a = b.selectionText;
...
// a few nested returns and functions ...
var h = g.Ab, c = h.concat(a); // => error mentioned above
var a = b.selectionText;
...
// a few nested returns and functions ...
var h = g.historyItems, c = h.concat(a);
事实上,这是一件小事
事实证明,selectionText
属性是在externs文件中定义的,而historyItems
是由我在chrome的存储中设置的。因此,相同风格的代码会导致不同的行为
<>强:>真正重要的是外部文件中定义的内容。请考虑将答案标记为解决方案:谢谢!需要等待几天才能让你接受自己的答案。