Chapter 3: Functions, Methods and Objects Flashcards
What is a function?
A grouped together series of statements that together perform a specific task.
Functions can be set to run when the user interacts with out site in a certain way.
Like with variables you declare functions.
If we are going to ask a task to run later we need to give our function a name and we then call it later
function updateMessage() { //some code }
updateMessage();
What’s a parameter? What’s an argument?
These are similar.
A parameter can be passed to a function to use in running the code.
An argument is a specific value passed to the function as a parameter
When do anonymous function run
As soon as the js interpreter comes across them
What does return do in a function
It will return a specific value out of a function.
As soon as return is run, the interpreter exits the function
How do you return multiple values out of a function?
You store the answers in an array and return the array out of the function
function getSize(width, height, depth) { var area = width * height; var volume = width * height * depth; var sizes = [area, volume]; return sizes; }
ar areaOne = getSize(3, 2, 3)[0];
ar volumeOne = getSize(3, 2, 3)[1];
expressions
expressions produce a value and can be used where a value is expected
What do js interpreters do when parsing code with named functions?
Looks for named variables and functions first before going through script line by line
function expression
You put a function where the interpreter expects an expression. They are normally anonymous functions as well.
In a function expression its not processed until the inteprter gets to it.
What are IIFE?
Immediately Invoked Function Expressions. These are run as soon as they are come crossed.
var area = (function() { var width = 3; var height = 3; return width * height; }());
commonly used as a wrapper around code. Due to scope creep issues, this is done to prevent variable name conflicts
What is variable scope
Scope is about where you name your variables (inside a function or outside it). If it’s inside a function its got local scope and can only be accessed by that function. If it’s declared outside a function its got global scope.
globals can be accessed from anywhere.
Why do we prefer local variables to globals?
Reduces risk of naming conflicts
Use less memory
Write an object using literal notation
var hotel = { name: 'Quay', rooms: 40, booked: 25, checkAvailibility: function() { return this.rooms - this.booked; } };
How can we access an object’s properties
using dot notation
hotel.name;
using square bracket notation
hotel[‘name’];
- the latter is used two cases: property name is a number (a bad idea) and when a variable is used in place of a property name
What is a . (not a, but the . after it)
. is the member operator. it shows that property or method is related to an object
What are object constructers?
It’s a way of creating similar objects by creating a function to create each new object.
the function is named with a capital to help identify it as a constructor object function
function Hotel(name, rooms, booked) {
this. name = name;
this. rooms = rooms;
this. booked = booked;
this.checkAvailibity = function() {
return this.rooms - this.booked;
};
}
You create an instance of an object using the function
var quayHotel = new Hotel(‘Quay’, 40, 25);