Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update: <code>@expose</code> is now deprecated. Answer updated with new method.</strong></p> <p>As you noted, <code>@const</code> has no impact on inlining. <code>@const</code> only means that the value doesn't change (is assigned/set only once).</p> <p><code>@nocollapse</code> was specifically created to prevent property collapsing. Given:</p> <pre><code>goog.provide('foo.bar'); /** @nocollapse */ foo.bar.baz = 42; alert(foo.bar.baz); </code></pre> <p>The purpose of <code>@nocollapse</code> in this case is to prevent the compiler from collapsing the property to something like:</p> <pre><code>var a = 42; alert(a); </code></pre> <p>However the compiler still knows from flow analysis that <code>foo.bar.baz</code> is assigned the integer <code>42</code> and never changed. Therefore it's going to just use the known values.</p> <p>The only reliable way to prevent inlining is to export the property:</p> <pre><code>goog.provide('foo'); goog.provide('foo.bar'); foo.bar.baz = 42; goog.exportProperty(foo.bar, 'baz', foo.bar.baz); alert(foo.bar.baz); </code></pre> <p>There is a known (albeit small) need to stipulate to the compiler that renaming is safe, but inlining is not. See <a href="https://code.google.com/p/closure-compiler/issues/detail?id=971" rel="nofollow">https://code.google.com/p/closure-compiler/issues/detail?id=971</a></p> <p>If you are not using closure library, the export would look like this:</p> <pre><code>var foo = {}; foo.bar = {}; /** @nocollapse */ foo.bar.baz = 42; foo.bar['baz'] = foo.bar.baz; alert(foo.bar.baz); </code></pre> <p>If your goal is to make sure <code>foo.bar.baz</code> isn't inlined OR renamed, then you would need either quoted properties and <code>@nocollapse</code>.</p> <pre><code>goog.provide('foo'); goog.provide('foo.bar'); /** @nocollapse */ foo.bar.baz = 42; goog.exportProperty(foo.bar, 'baz', foo.bar.baz); alert(foo.bar.baz); </code></pre> <p>If you are providing this namespace to others for use, then the best case is a combination of <code>@nocollapse</code> to prevent collapsing and allow external updating and <code>goog.exportSymbol</code>.</p> <pre><code>goog.provide('foo'); goog.provide('foo.bar'); /** @nocollapse */ foo.bar.baz = 42; goog.exportSymbol('foo', foo); goog.exportProperty(foo, 'bar', foo.bar); goog.exportProperty(foo.bar, 'baz', foo.bar.baz); alert(foo.bar.baz); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload