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);
How can I add a property to an object?
Use dot notation
hotel.spa = true;
How do you delete a property/method from a object?
Use the delete keyword
delete hotel.booked;
What does the this keyword do?
It’s commonly used in functions and objects. It always refers to one object (where its used though affects what object is selected).
Used in a function with global scope = works with window object
Global variables = this refers to window object
As a method of an object = this refers to that object
How are arrays and objects connected?
Arrays are objects. You can store either in the other
What built in objects are available on a webpage?
Browser Obkect Model; Document Object Model and Global Javascript Objects
What is an Object Model?
An Object model is a group of objects, each of which represent related things from the real world. Together they form something larger.
What does window.innerheight mean?
Browser object to model height of window (excluding browser chrome/interface)
what does window.pageXOffset mean?
How far across the page the user has scrolled
What does window.screenX mean?
x coordinate of the pointer relative to top left hand corner.
What does window.screen.width mean?
Access width of screen in pixels
What do these string methods do: toUpperCase(); toLowerCase(); charAt(); indexOf(); lastIndexOf(); substring(); split(); trim(); replace();
- changes strings to uppercase
- changes strings to lower case
- returns the character at a certain position (given as an argument)
- Returns the index number of the first character or set of characters found within the string
- Returns the index number of the last character or set of characters found within the string
- returns characters found between two index numbers
- When a attractor is specified, it splits the string each time that character is found, then stores the result in an array
- trim: removes whitespace from start and end of a string
- takes one value that should be found and replaces with another (defaults to only finding first match).
How can I check if something is a number?
isNan()
What does toFixed() and toPrecision() do?
These methods return a string
toFixed: rounds to specific number of places
toPrecision: Rounds to total number of places
What does toExponentiaL() do?
Returns a string to represent the number exponential notation . Javasgript can only model so many numbers with decimal points
What do the following Math object methods do:
.round() .sqrt(n) .ceil() .floor() .random()
round - rounds to nearest integer sqrt - returns squarer of argument ceil - rounds up to nearest integer floor - rounds down to nearest integer random- generates a random number between 0 (inclusive) and 1 (not inclusive)
What formula can calculate a random number between 1 and 10?
Math.floor((Math.random()*10) + 1);
How do you create a date with today’s current date and time?
Use the date constructor
var today = new Date();
How are dates stored in javascript?
As a number. Milliseconds since 1st Jan, 1970
What are three ways to set date and time?
new Date(1996, 11, 26, 15, 45, 55); new Date('Dec 26, 1996 15:45:55') new date(1996, 11, 26)
How do you for a date:
get a date or set a date
get the Day
get year
getDate() / SetDate() - returns / sets day of the month, 1-31
getDay() - returns the day of the week 0-6
getFullYear() / setFullYear() - returns / sets the year (4 digit number)
How do you for a time: get the hour get millisecond get minutes get month get seconds get the time
getHour / setHour - returns / sets hour 0-23
getMilliseconds / setMilliseconds - returns sets the milliseconds 0-999
getMinutes / setMinutes - returns / sets the minute 0 -59
getMonth / setMonth - returns sets month 0 -11
getSeconds / setSeconds - returns - sets seconds 0 -59
getTime / setTime - returns/sets time in milliseconds since Jan 1st, 1970 00:00:00 UTC