Week 3 Class Note
Week 3 Class Note
WEEK 03
INSTRUCTORS:
OFURUM CHIZITERE || chizzy.o@deebug.org
OGHAMOMWAN SAMUEL || samuel@deebug.org
Objects, Loops, Functions
JavaScript Objects
JavaScript is all about objects, a large part of what makes JavaScript so dynamic are objects.
If you understand objects, you pretty much understand JavaScript.
In JavaScript, almost "everything" is an object.
Object Properties
A JavaScript object is a collection of named values.
The named values, in JavaScript objects, are called properties.
The properties in the above object are;
firstName, lastName, age, eyeColor.
Creating Objects
This example creates an empty JavaScript object, and then adds 4 properties:
JavaScript Objects are Mutable
Objects are mutable: They are addressed by reference, not by value.
If person is an object, the following statement will not create a copy of person:
The object x is not a copy of person. It is person. Both x and person are the same object.
Any changes to x will also change person, because x and person are the same object.
Arrays vs Objects
In JavaScript, arrays use numbered indexes.
In JavaScript, objects use named indexes
Example
In the following example, the code in the loop will run, over and over again, as long as a
variable (i) is less than 10:
While Loop
The while loop loops through a block of code as long as a specified condition is true.
Note: If you forget to increase the variable used in the condition, the loop will never end.
This will crash your browser.
The Do While Loop
The do while loop is a variant of the while loop. This loop will execute the code block once,
before checking if the condition is true, then it will repeat the loop as long as the condition
is true.
The example below uses a do while loop. The loop will always be executed at least once,
even if the condition is false, because the code block is executed before the condition is
tested:
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
The For in Loop
The JavaScript for in statement loops through the properties of an Object:
Array.forEach()
The forEach() method calls a function (a callback function) once for each array element.
Note that the function takes 3 arguments:
The item value
The item index
The array itself
The example above uses only the value parameter. It can be rewritten to:
variable - For every iteration the value of the next property is assigned to the variable.
Variable can be declared with const, let, or var.
iterable - An object that has iterable properties.
Browser Support
For/of was added to JavaScript in 2015 (ES6)
JavaScript Functions
Most programming languages provide built in functions; these are functions which have
already been defined by the creators of the programming language to carry out specific
tasks. These functions usually carry out commonly needed operations, which would
normally require several steps/lines of code to accomplish. It is good practice to have a
good knowledge of as many of the built in functions as possible available to you in
JavaScript. This will invariably save you a lot of time.
For instance, computing the square root of a number. In general, we as programmers don’t
really care about how the function for finding the square root of a number does what it
does, only that it “does it”.
One of the best things about functions is that they allow you as a programmer to reuse
code, instead of rewriting it from scratch.
Another great thing about functions is that functions allow us to keep our variable
namespace clean. What this means is that local variables only “live” as long as the function
does. Therefore, function_a can have and use a variable called I, and function_b can also
use a variable called I and there will be no confusion because each variable I only exists
when the computer is in the process of executing that specific function.
Carefully define the data which might need to come into the function from the caller in the
form of parameters.
Also carefully define what data variables might be needed within the function to
accomplish its task.
Following our black box example in the beginning of this course, functions can be called
black boxes because we don’t necessarily need to know how they work, just what is
supposed to go into them to enable them carry out their operations successfully, and what
is supposed to come out of them.
Parts of a function
When defining a function, we must spell out the following attributes of the function;
The Name: This describes the purpose of the function. Usually some sort of verb or
phrase, such as “signUp”, or “play”.
The Inputs: called parameters. These describe what data is needed by the function for it to
work.
The calculation/statements: This varies for each function.
The output: Value/values that are computed inside the function and “returned”.
Function Workspace
Every function has its own Workspace. This means that every variable inside the function is
only usable during the execution of the function (and then the variables go away).
Having a separate "workspace" for each function is critical to proper software engineering.
If every function shared every variable in an entire program, it would be easy to
inadvertently change the values of variables that you shouldn't. Further, it would be hard to
remember what "names" have been used elsewhere, and coming up with new names to
represent similar ideas would be challenging.
A side-effect of function variables not existing after the end of the function is that the only
way to get information "out" of a function is by "returning" that information via the output
of the function.
Additionally, the function can only "see" the information that is "passed" to it via
parameters. Thus the only way information can get "in" to the function is by using
parameters.
Note: In certain object oriented languages (e.g., C++, Java, ActionScript), a function can also
see all of the variables associated with its containing object.
Function syntax
Function Example
After a function expression has been stored in a variable, the variable can be used as a
function:
Function Hoisting
Hoisting is JavaScript's default behavior of moving declarations/definitions to the top of the
current scope.
Hoisting applies to function declarations/definitions.
Because of this, JavaScript functions can be called before they are declared: