defining the prototype in a function enclosure seems to break warnings

50 views
Skip to first unread message

codefactor

unread,
Dec 19, 2011, 3:13:58 PM12/19/11
to Closure Compiler Discuss
At my company we use primarily object oriented javascript. Usually
objects will have private static variables or functions that are
hidden from the global scope.

Here is an example:

/** @constructor */
function A() {}

(function(){
function private_fn(num){
alert(num);
}
/** @param {number} num A number */
A.prototype.foo = function(num) {
private_fn(num);
}
})();

new A().foo("Hello"); // Why does this not give me a warning?? I
defined the parameter as a number.

Nick Santos

unread,
Dec 19, 2011, 3:33:07 PM12/19/11
to closure-comp...@googlegroups.com
See:
http://code.google.com/p/closure-compiler/issues/detail?id=61

I believe it will work if you put the call "new A().foo('hello')" into
a function (any function).

The basic problem is that type inference could literally take forever
(i.e., you can rigorously prove that no matter how good the type
inference is, the compiler will always be able to find more type
information). So there has to be some point where Closure Compiler
throws up its hands and says it's going to stop trying.

The general (over-simplified) rule of thumb is that it will give up
whenever it has to jump back and forth between the global scope and a
local scope. (i.e., if a property in the global scope needs something
in a local scope which needs something in the global scope).

Nick

Nick Santos

unread,
Dec 19, 2011, 3:37:13 PM12/19/11
to closure-comp...@googlegroups.com
On Mon, Dec 19, 2011 at 3:33 PM, Nick Santos <nicks...@google.com> wrote:
> See:
> http://code.google.com/p/closure-compiler/issues/detail?id=61
>
> I believe it will work if you put the call "new A().foo('hello')" into
> a function (any function).
>
> The basic problem is that type inference could literally take forever
> (i.e., you can rigorously prove that no matter how good the type
> inference is, the compiler will always be able to find more type
> information). So there has to be some point where Closure Compiler
> throws up its hands and says it's going to stop trying.
>
> The general (over-simplified) rule of thumb is that it will give up
> whenever it has to jump back and forth between the global scope and a
> local scope. (i.e.,  if a property in the global scope needs something
> in a local scope which needs something in the global scope).

Or, perhaps, to put this more simply, it will give up if it finds a
cycle between two scopes.

Nick

codefactor

unread,
Dec 19, 2011, 4:25:24 PM12/19/11
to Closure Compiler Discuss
Thank you Nick!

Yes - it will work if I do this.

function test() {
new A().foo("Hello"); // This now gives me the warning

Reply all
Reply to author
Forward
0 new messages