ClojureScript and closure: how to protect attributes from being renamed by closure

ぃ、小莉子 提交于 2019-12-07 08:46:11

问题


I am trying to to some HTML5 canvas drawing and I ran across a problem with the advanced compilation mode. I would like to exemplify this with the mozDash property of Mozilla browsers (although this question is quite generic on the attribute optimization feature) https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D#Gecko-specific_attributes

The javascript canvas.mozDash = ... code can be expressed as [1] (set! (.-mozDash canvas) ...) or [2] (aset canvas "mozDash" ...) in Clojurescript.

I had used [1] before and it worked in most cases, however with the mozDash attribute the mozDash identifier is gone in the advanced compilation result. Therefore I tried [2] and it seems that the mozDash identifier is retained using the aset variant.

My questions therefore are:

  • Is this an intended difference of these notations?
  • Why is the behaviour different ([1] and [2] work) for (.-fillStyle canvas)?

I kind of suspect that standard HTML properties are protected by default while not-standard properties (like mozDash) is not supported.


回答1:


The closure compiler is allowed to rename directly accessed attributes that aren't specified in externs or exports.

See https://developers.google.com/closure/compiler/docs/api-tutorial3#propnames

Specifically, (aset x "y" z) translates to x["y"] = z, which is exempt from minimizing, while (set! (.-y x) z) translates to x.y = z and can be minimized unless x.y is specified as extern or exported.

I would assume that the mozDash property isn't specified in the externs file(s) you're using for Canvas.



来源:https://stackoverflow.com/questions/17144429/clojurescript-and-closure-how-to-protect-attributes-from-being-renamed-by-closu

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