google closure compiler - @const in javascript? - Stack Overflow

I was reading .xml when I noticed in the Constant section with the annotated ments:*** The number of s

I was reading .xml when I noticed in the Constant section with the annotated ments:

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

and then the guide goes on with "This allows the piler 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!

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

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

and then the guide goes on with "This allows the piler 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!

Share Improve this question edited Aug 6, 2010 at 7:50 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked Aug 6, 2010 at 4:59 MathGladiatorMathGladiator 1,2111 gold badge10 silver badges24 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

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

For example, if you try to pile 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.

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 remends 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 "pile" some code using Google's Closure Compiler that "piler" will look at such things in the ment blocks and even generate warnings and errors. But this is separate to running the code unmodified in an actual Javascript interpreter.

const (MDC)

And: const (the page you linked...)

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

es 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""};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745334323a4623032.html

相关推荐

  • google closure compiler - @const in javascript? - Stack Overflow

    I was reading .xml when I noticed in the Constant section with the annotated ments:*** The number of s

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信