The post you've linked to is using TypeScript, which is a different language from JavaScript: It's a superset of JavaScript (which compiles to JavaScript) that adds static typing (and a couple of other features, like automatic property initialization in constructors).
This TypeScript line:
var ItemsArray : ItemModel[];
is equivalent to this JavaScript:
var ItemsArray;
It's just that the TypeScript version declares that the ItemsArray
variable has the static type ItemModel[]
.
If you're using JavaScript itself, you don't define the types of variables. Your export var ItemsArray = [];
is just fine (other than that I'd use let
or const
instead of var
).
Side Note: You can write that array more concisely in JavaScript using an array initializer:
export const ItemsArray = [
new ItemModel(0, "BreakFast"),
new ItemModel(1, "Lunch"),
new ItemModel(2, "Dinner")
];
That's also valid TypeScript, even without a type declaration: TypeScript will infer that you want the type of ItemsArray
to be ItemModel[]
(because TypeScript does type inference and it can see that you're filling ItemsArray
with ItemModel
objects).
If you wanted to start with an empty array but still give it a static type, you'd do this:
// /------ Telling TypeScript to use ItemModel[]
// vvvvvvvvvvvvv as the type
export const ItemsArray : ItemModel[] = [];
// ^^^^^-- Initializing ItemsArray with an
// empty array
Then your push
calls would be typechecked, so this would work:
ItemsArray.push(new ItemModel(0, "BreakFast"));
but this would cause an error (during TypeScript->JavaScript compilation)
ItemsArray.push("BreakFast");
because "Breakfast"
is not compatible with ItemModel
.
Side Note 2: The overwhelming convention in JavaScript (and TypeScript) is that variables start with a lowercase letter, not an uppercase one. So itemsArray
rather than ItemsArray
.