C++ features in js.

is there equivalent in js of c++ const definition and array initialize?
in specific,
1.
if i want to define a const like

var MAX_NUM = 6;

is there a way to prevent it from being modified?
furthermore, can i prevent it from being showed in the inspector window?
2.
how to initialize a class array with const data? like this in c++:

class foo
{
int a;
int b;
};
foo myArray[10] = {
{0, 0},
{1, 1},
...
};

Don’t know, use C# it’s more pro :smile:

  1. const int MAX_NUM = 6;

  2. int[ ] numbers = new int[5] {1, 2, 3, 4, 5};

I don’t think this is possible in Unity Script (Javascript constants would have ‘var’ replaced by ‘const’, but this does not compile in Unity). If you really need constants, you will have to switch to using C#. Constants are automatically hidden in the inspector (in fact cannot be shown), but if you wish to hide a public variable, you can use the HideInInspector attribute:

Initializing base type arrays is possible, but class type arrays is not. C# does support custom class initializers but not in the mono-version used by Unity.

If you really want constants in Javascript/Unityscript, you could probably create your own Constant class, or maybe fake it with enums.

If you don’t want it to show in the inspector type “private” before the var ex:

private var money : int = 0;//Or whatever else you want.

That will make it not show in the inspector.