-
Notifications
You must be signed in to change notification settings - Fork 4
WritingApps
A computer program, is a sequence of instructions, written to perform a specified task or tasks on a computer.
We write programs using a programming language, and a high level language like JavaScript allows us to express our ideas more easily because the syntax is more like the English language than older languages that were modeled on binary switches, combinations of zeros and ones. When we write programs, we describe objects by specifying their properties and defining their behaviours, that is, what they're made of and what they can do and how they interact with other objects. Then we create objects and get them to interact with each other - simple, no?
Like many languages, programs written in Javascript are executed in a special runtime environment or interpreter, or sometimes what is called an engine.
Similar to the way in which a person reads a book, a JavaScript interpreter reads the programs you've written line by line, holding objects in memory as they are introduced on each line. When you read a book, or someone tells you a story, you hold the characters and events that transpire in your memory, and once the characters are in your memory and they participate in events, the story begins to make sense and unfold.
Same thing with a computer program. There's some pre-scanning that happens though: for example, and very simply put, function definitions within the scope current object are hoisted - pulled up into memory - before the execution of statements, even though these functions may be defined later in your page of code. You might compare this to the way in which the director of a play will want to know all of the actors within a scene, even though they are not on stage from the beginning of the act.
The word runtime is used to describe the environment in which your application is run, and in the case of JavaScript, it is an intepreter of some variant that, at runtime, reads your source text - the code you've written - and then executes statements line by line. And when we're running the application, we say, "At runtime...". This is the case in variation for many languages, Java, Python, etc.
When we ask the interpreter to run our program, it looks at all the objects we've described, and line by line trys to make them interact with each other by reading our statements and evaluating the expressions we've written.
You can think of statements as stating something, sort of like sentences or even paragraphs, which can include expressions. And in JavaScript, we end statements with a semi-colon ;
:
// a statement:
var count = 0;
// another statement, including an expression:
var
nameFirst = "Jack",
nameLast = "Jones",
fullName = nameFirst + ' ' + nameLast;
console.log(fullName); // prints Jack Jones
These are both statements, they both state that the variables count
, nameFirst
and nameLast
exist and are initialized with some values, but the second statement creates three variables in the same statement and includes an expression, fullName = nameFirst + ' ' + nameLast
, whereas the first statement simply declares a single variable.
You can view expressions as a value first requiring evaluation, but that's open to interpretation. So, in the above example, the iterpreter had to evaluate the value of nameFirst
, the value of a String literal " "
, and the value of nameLast
in order to concatenate them into a single String to become the value of fullName
.
Everything in JavaScript is an Object, or can be treated as one, including primative types, like numbers and strings. An Object is the basic thing from which all other things are dirived. We can even create blank objects, think of them like empty containers, or a blob of clay which you can shape and define even while the program is runnning. Or before the program is running, you can define a type of Object, like a Person or a Car type of Object. Essentially, objects are merely key - value pairs, which means, everything we put inside an Object is named - the key - and by that name we can access the value.
The more specific we need an Object to be, the more we need to describe it for the interpreter. A Car is fairly generic, whereas a Lamborghini, much more speicific and awesome.
For everything we need our application to do, we build types of objects that can do specific things, and interacting together, these objects are our application.
© 2014-2015 Operation Spark