When I put a comment above my variable or function with @private in it what is it actually doing? I have looked at the documentation but I’m still not sure.
goog.provide('myproject');
/** @private */
myproject.foo = "bar";
I can still access it when I open up chrome’s development tools (myproject.foo). And…
goog.require('myproject');
window.addEventListener('load', function() {
//this works.
document.body.textContent = myproject.foo;
});
The above code still set’s the bodies textContent to equal “bar”, even when compiled. So what does @private actually do?
The access control annotations
@private,@protected, and@publicaredirectives for the Closure Compiler that help developers enforce the
desired level of visibility for properties and functions.
To emit warnings for access violations use the Compiler flag:
To emit errors for access violations use the Compiler flag:
Access control annotations are enforced on a per file basis,
meaning that any properties annotated
@privateor@protectedmay beaccessed anywhere within the same file. Also note that the compiler removes
these annotations from the compiled code since they are not part of the
JavaScript language.
Example
file1.js
file2.js
With the flag
--jscomp_error=visibilityset, Closure Compiler emits thefollowing errors.
See Visibility (private and protected fields) in the Google JavaScript
Style Guide.