0

I want to use string constants instead of direct strings across my Javascript objects (when defining parameter keys, etc.). The purpose is to have my code indexed by IDE, have suggestions, auto-corrections / completitions, all the error-proof goodies.

I want to define explicit keys for the constructor of this class EPPZObject, so I did:

var EPPZ =
{
    width: 'width',
    height: 'height'
}

var EPPZObject = Class.extend
({

    construct: function(parameters)
    {
        // Constant usage works fine here.
        log(parameters[EPPZ.width]);
        log(parameters[EPPZ.height]);
    }

});

In client code, I can use the constants as well:

log(EPPZ.width); // Logs 'width' just fine.
log(EPPZ.height); // Logs 'height' just fine.

But when I want to use the constats while constructing (that would be the whole point actually), then it just not works:

var objectThatWorks = new EPPZObject(
        {
            'width' : '9',
            'height' : '9',
        });

var objectThatNotWorks = new EPPZObject(
        {
            EPPZ.width : '9',
            EPPZ.height : '9',
        });

It says:

Uncaught SyntaxError: Unexpected token . 

How to get over this? Is there a similarly clean solution to use constants?

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • Maybe the question is `How to use expressions as associative array keys at declaration time?` – Geri Borbás Jan 02 '14 at 23:57
  • You cannot use variables in object literals. Maybe you should look for some makro preprocessor, instead of defining the names as js variables. Those constants will never change, so why make them dynamic? – Bergi Jan 03 '14 at 00:00
  • 1
    I just want to have my IDE (PHPStorm) to auto-correct / auto-suggest when typing constructors. I want all the parameter keys indexed. I bet a "Foreign" preprocessor won't do this for me. – Geri Borbás Jan 03 '14 at 00:07
  • There is not such thing as Class in JavaScript – Alex Shilman Jan 03 '14 at 00:07
  • @Alex: function Class(){}Class.prototype.construct=function(){};Class.extend=function(e){var t=function(){if(arguments[0]!==Class){this.construct.apply(this,arguments)}};var n=new this(Class);var r=this.prototype;for(var i in e){var s=e[i];if(s instanceof Function)s.$=r;n[i]=s}t.prototype=n;t.extend=this.extend;return t} – Geri Borbás Jan 03 '14 at 00:08
  • Yes I know. But that's customized. By default JavaScript does not have a Class object. – Alex Shilman Jan 03 '14 at 00:11
  • @Geri: Do you need to pass objects to your constructors? It might be easier [to type] if you just had multiple parameters. Your IDE should be able to show you their names and docs when calling the constructor. – Bergi Jan 03 '14 at 00:11
  • @AlexShilman: Does it matter? It's not in the scope of this question. – Bergi Jan 03 '14 at 00:12
  • Yap, true. The question is not quiet specific to my original problem. Gonna edit soon. Docs will do this for now, thanks guys. – Geri Borbás Jan 03 '14 at 00:16
  • what about parameters[EPPZ['width']] – Alex Shilman Jan 03 '14 at 00:19
  • I can use it fine while referencing, the problematic part is parameter object's declaration time. – Geri Borbás Jan 03 '14 at 00:41
  • Actually I just prefer readable / explicit / error-proof class interfaces instead of documentations / comments, "loose" parameters, paremeter lists and so. It is quiet painful to build up a design in Javascript after hanging around with Objective-C. Even PHP have better patterns. :( – Geri Borbás Jan 03 '14 at 00:49

2 Answers2

0

Cant you make a Singleton like this?:

var EPPZ = {
        width : '9',
        height : '9'
};

and then maybe

var objectThatWorks = new Object();
objectThatWorks.width = EPPZ.width;
objectThatWorks.height = EPPZ.height;

EDIT

Did you tried prototyping?

EPPZObject.prototype.width = EPPZ.width;
Santiago Rebella
  • 2,399
  • 2
  • 22
  • 29
  • I'm to use constants for keys, not for values. But this is something close to I just came up with -> using an intermediate object. – Geri Borbás Jan 03 '14 at 01:11
0

Can be worked around in a really weird way. Not sure if I'm gonna use this in production yet.

Using a helper object that "translates" parameters into an object:

var Parameters = Class.extend
({
    construct: function(parameters)
    {
        for (var index = 0; index < parameters.length; index += 2)
        { this[parameters[index]] = parameters[index+1]; }
    }
});

Now I can construct objects almost the way I wanted...

var object = new EPPZObject(new Parameters(
[
    EPPZ.width, 8,
    EPPZ.height, 9,
]));

...but added some boilerplate as well :(

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172