这个问题有点老了,但是当前版本的 Closure Compiler 可以const
很好地处理关键字,因为它只会替换它var
并理解变量是常量。
例如,使用高级模式 ( --compilation_level ADVANCED_OPTIMIZATIONS
) 和 polyfill 重写 ( --rewrite_polyfills
,这很重要,如前所述,“[不要使用]const
关键字,因为它不是 ECMAScript 标准的一部分”,但这会重写它var
以便它可以很好地播放使用较旧的浏览器)
const get_selected_text = (/** @return {function():string} */ function() {
if (window.getSelection || document.getSelection) {
return function () {
const selection = /** @type {function():Object<string,?>} */ (window.getSelection || document.getSelection)();
if (typeof selection['text'] === "string") {
return selection['text'];
} else {
return selection.toString();
}
};
} else if (document.selection && document.selection.type !== "Control") {
return function () {
return document.selection.createRange().text;
};
}
return function () {
return '';
};
})();
出来看起来像
var i=window.getSelection||document.getSelection?function(){var a=(window.getSelection||document.getSelection)();return"string"===typeof a.text?a.text:a.toString()}:document.selection&&"Control"!==document.selection.type?function(){return document.selection.createRange().text}:function(){return""};