JavaScript 中的@const?

发布于 2024-09-13 03:04:47 字数 459 浏览 3 评论 0原文

我正在阅读 http://google-styleguide.googlecode.com/svn/trunk /javascriptguide.xml 当我注意到常量部分带有带注释的注释时:

/**
 * The number of seconds in each of the given units.
 * @type {Object.<number>}
 * @const
 */

然后指南继续“这允许编译器强制执行常量性”。

这是v8的东西吗?这是在哪里记录的?

我对也许,只是也许,我可以提供带有类型信息的 v8(或其他)的可能性感到头晕!

I was reading http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml when I noticed in the Constant section with the annotated comments:

/**
 * The number of seconds in each of the given units.
 * @type {Object.<number>}
 * @const
 */

and then the guide goes on with "This allows the compiler to enforce constant-ness."

Is this a v8 thing? where is this documented?

My mind is giddy with the possibility that maybe, just maybe, i can provide v8 (or whatever) with type information!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

記柔刀 2024-09-20 03:04:47

Google 的闭包编译器可以接受JSDoc 注释,它会生成警告或错误。

例如,如果您尝试使用高级优化来编译以下代码>:

/** @const */ var MY_BEER = 'stout';

MY_BEER = 'bar';

会产生错误:

错误数量:1

JSC_CONSTANT_REASSIGNED_VALUE_ERROR:常量 MY_BEER 在第 5 行字符 8 处多次赋值

他们不鼓励使用 const 关键字,因为它不是 ECMAScript 标准的一部分。

The Google's Closure Compiler can accept JSDoc comments, and it will generate warnings or errors.

For example, if you try to compile the following code with the Advanced Optimization:

/** @const */ var MY_BEER = 'stout';

MY_BEER = 'bar';

It will generate an error:

Number of errors: 1

JSC_CONSTANT_REASSIGNED_VALUE_ERROR: constant MY_BEER assigned a value more than once at line 5 character 8

They discourage the const keyword because it is not part of the ECMAScript Standard.

天煞孤星 2024-09-20 03:04:47

Javascript 中有一个 const 关键字,但 Internet Explorer 无法识别它,因此您无法真正使用它。

这就是为什么 Google 的 Javascript 样式指南建议使用大写字母作为常量,或者将 @const 放入文档块中。

这两种技术仅供参考,对代码没有实际限制。

请注意,当您使用 Google 的 Closure 编译器 该“编译器”将查看注释块中的此类内容,甚至生成警告和错误。但这与在实际的 Javascript 解释器中运行未修改的代码是分开的。

There is a const keyword in Javascript, but it isn't recognised by Internet Explorer so you can't really use it.

That's why Google's Javascript style guide recommends either using upper-case letters for a constant, or placing @const into a documentation block.

Both these techniques are advisory only and place no actual restrictions on the code.

Note that when you "compile" some code using Google's Closure Compiler that "compiler" will look at such things in the comment blocks and even generate warnings and errors. But this is separate to running the code unmodified in an actual Javascript interpreter.

几味少女 2024-09-20 03:04:47

这个问题有点老了,但是当前版本的 Closure Compiler 可以很好地处理 const 关键字,因为它只会用 var 替换它并理解该变量是一个常数。

例如,对于高级模式(--compilation_level ADVANCED_OPTIMIZATIONS)和polyfill重写(--rewrite_polyfills),这个很重要,正如前面提到的,“[不要使用] < code>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""};

This question is a bit old, but the current version of Closure Compiler will handle the const keyword quite well, as it will just replace it with var and understand that the variable is a constant.

For example, with advanced mode (--compilation_level ADVANCED_OPTIMIZATIONS) and polyfill rewriting (--rewrite_polyfills, this one's important, as mentioned earlier, "[don't use] const keyword because it is not part of the ECMAScript Standard", but this rewrites it as var so that it plays nicely with older browsers)

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 '';
    };
})();

comes out looking like

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""};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文