First thing we start off with is, we create a variable here called Render Engine Class
and you define a variable by starting with the keyword var, and then
the variable name, and then you say equals, whatever you would like it to be equal to.
In this case we use an opening brace, to indicate that we'd like this to be
a javascript object. This is the object literal notation, for defining an object
in Javascript. From there we can declare methods and member variables of that
object by specifying the name of the member, a colon and then the value that its
going to take. In this case we declare the method of the name
drawString, we use a colon and then we say that this is going to be a function, we use the
keyword function to define this and then we give a list of all the parameters
and then open the function with an opening brace, similarly to how you would
in many other languages. Now, again, we create a variable, name it, and then assign
to it, the value of gRenderEngine.context. Now this is going to take the
JavaScript object, gRenderEngine and the dot notation is going to tell us that
we want the context member of this object and we do something similar down here
where we, grab the font member of the ctx object
and assign it this value here. Now you notice that we're using addition in strings: addition is over loaded in
javascript to also designate string concatenation, again
similar to a lot of other languages you might have seen. Next we have an example of an if statement.
If statements work essentially the same way that they would in any other language,
where you have the keyword if, then in parenthesis you specify the
predicate that's going to return a boolean value, either true or false and then
the body of the if statement that executes, if that evaluates to true. Now, note
that the statement here doesn't have braces around it designating what executes
if the if statement evaluates to true. This is similar to some other languages
where, if there's a single line that you need to evaluate, you don't require
those braces. We use this here there, just for readability purposes. So you
might see this in the code. Now for larger f blocks, you can delineate them with
a opening brace, and then once they are done, way down here, you close them
with another brace. From there we have a number of more examples of declaring and
setting variables and then we come to a for loop. For loops are structured
essentially the same way they are in C, were we have an initializer value, then
a predicate to evaluate for, when the for loop should end, and then an expression
to execute at the end of each for loop. And similarly to the if blocks, the
block of code that you'd like to execute each time the for loop runs, is
delineated by braces. Although again, if it's a single line, you don't require
those braces to be there.
Now here we see words n where n is in square braces and again, n is the
variable that our for-loop is looping over. This is an example of accessing a
list, where n is the n-th element in that list. Now, like most other languages,
this is zero indexed as well. So keep that in mind.