问题
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