0
define(["dojo/_base/declare"], function (declare) {
    // module:
    //      StyleNames
    // summary:
    //      Style element names.
    var StyleNames = declare(null, 
        {
            PROP1: "Style1",
            PROP2: "Style2"
        }
    );

    StyleNames.dataContexts = [
        StyleNames.PROP1,
        StyleNames.PROP2,
    ];

    return StyleNames;
});

var styleNames = new StyleNames();
console.log("PROP1 is defined as: ", styleNames.PROP1); // prints "Style1"
console.log("dataContexts is undefined: ", styleNames.dataContexts); // Error: styleNames.dataContexts is undefined

My setup was similar to https://stackoverflow.com/a/11329956/1610451 but looking in the DOM, I can see that it is set to the constructor styleNames.constructor.dataContexts with undefined values.

Backstory: I'm porting ActionScript code to JavaScript and need to maintain existing interfaces. An example of the ActionScript code that I'm porting is:

public class StyleNames {
    public static const PROP1 : String = "Style1";
    public static const PROP2 : String = "Style2";

    public static const dataContexts : Array = [
        PROP1,
        PROP2
    ];

    public function StyleNames(){}
}
Community
  • 1
  • 1
Bailey Hayes
  • 13
  • 1
  • 3

2 Answers2

0

You've got it right in your declaration. That is the most unambiguous way to define static variables in my opinion. In your console.log code, you're just not referring to the dataContexts variable correctly. It's a static variable, so it does not belong to any one instance. You want to refer to it this way: StyleNames.dataContexts.

Edit: you've also got a possible syntax error in there. Take out the second comma under the StyleNames.dataContexts declaration:

StyleNames.dataContexts = [
    StyleNames.PROP1,
    StyleNames.PROP2
];

Access the variable like this:

StyleNames.dataContexts

as opposed to this.dataContexts or instanceRef.dataContexts

mschr
  • 8,531
  • 3
  • 21
  • 35
Zach Shipley
  • 1,092
  • 6
  • 5
  • Although the Trailing Comma of Death is truly to be feared, there are actually no modern browsers that choke on it, and IE7 or 8 "fixed" this deadly "bug". Still, it is better to be careful. But modern browsers don't care (nor does PHP, for example). – Jared Farrish Aug 19 '12 at 21:39
  • Good point, @JaredFarrish. I knew I had been bitten by that before, but I couldn't recall which browsers would choke on that. – Zach Shipley Aug 19 '12 at 21:40
  • Thank you for the response! I left out that I had tried referencing it as a static ("StyleNames.dataContexts") but that gives an undefined error as well. I also tried creating an instance variable in a constructor but setting the values outside the declare. Removing the extra comma didn't help either :( – Bailey Hayes Aug 19 '12 at 23:04
  • youre correct, and so is OP. In the declaration, a simple variable referenced by `eval(declaredClass).StaticName` is created this way. But there's no inheritance relation between an instance of said module and its 'static' contents. – mschr Aug 20 '12 at 08:32
  • Got it, thank you! The trick was to place the other vars as static (which is what they should have been anyways). I thought I had already tried that, but oh well that's what I get for working on the weekend. @phusick answer is correct, but this answer was right from the start. – Bailey Hayes Aug 20 '12 at 13:20
0

This is how it is done right in my way of thinking:

require(["dojo/_base/declare"], function(declare) {

    var StyleNames = declare(null, {

        constructor: function() {
            // a.k.a public function StyleNames(){}
        },

        // non-static properties and functions here

    });

    // static properties and functions
    StyleNames.PROP1 = "Style1";
    StyleNames.PROP2 = "Style2";

    StyleNames.dataContexts = [
        StyleNames.PROP1,
        StyleNames.PROP2
    ];

    console.log("PROP1: ", StyleNames.PROP1);
    console.log("dataContexts: ", StyleNames.dataContexts);

});​

See a working example at jsFiddle: http://jsfiddle.net/phusick/6nfhJ/

phusick
  • 7,342
  • 1
  • 21
  • 26