Re: [closure-compiler-discuss] access "static class fields" from constructor?

16 views
Skip to first unread message

John Lenz

unread,
May 16, 2013, 11:48:39 PM5/16/13
to closure-compiler

This should work fine. Just define it after the constructor as usual. Have you tried? The name won't be referenced until the constructor is called and by then the property will exist. If the compiler is complaining, that is a bug.

On May 16, 2013 6:15 PM, "Erik Neumann" <er...@myphysicslab.com> wrote:

In a constructor, it seems there is no way to refer to a "static class field" (in the Java sense) because the class object doesn't yet exist.  Is that right?  I can get by with some workarounds like: define the static field on some other class;  but these are sub-optimal.  Here is an example of what I'd like to be able to do:


/**  "static class field"

* @type {number}

* @const

*/

fooPackage.barClass.QUUX = 3;


/**

* @constructor

*/

fooPackage.barClass = function() {

  /**

  * @type {number}

  */

  this.bazField = fooPackage.barClass.QUUX;

};



Thanks,

ErikN

--
 
---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Erik Neumann

unread,
May 17, 2013, 1:50:07 AM5/17/13
to closure-comp...@googlegroups.com
Wow, you are right, it works no problem.  So I guess Javascript does "lazy compiling".  Or maybe "lazy interpreting".

Thanks!
--ErikN

Tom Payne

unread,
May 17, 2013, 3:24:40 AM5/17/13
to closure-comp...@googlegroups.com
On 17 May 2013 07:50, Erik Neumann <er...@myphysicslab.com> wrote:
> Wow, you are right, it works no problem. So I guess Javascript does "lazy
> compiling". Or maybe "lazy interpreting".

No.

I assume that somewhere you have
goog.provide('fooPackage.barClass');
which defines fooPackage.barClass.

Then:
>>> fooPackage.barClass.QUUX = 3;
defines fooPackage.barClass.QUUX.

Then
>>> fooPackage.barClass = function() {
*redefines* fooPackage.barClass which replaces the original barClass
with a new one which does not have QUUX defined.

This is JavaScript 101.
Reply all
Reply to author
Forward
0 new messages