Chapter 3: Functions, Methods and Objects Flashcards

1
Q

What is a function?

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s a parameter? What’s an argument?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When do anonymous function run

A

As soon as the js interpreter comes across them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does return do in a function

A

It will return a specific value out of a function.

As soon as return is run, the interpreter exits the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you return multiple values out of a function?

A

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];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

expressions

A

expressions produce a value and can be used where a value is expected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do js interpreters do when parsing code with named functions?

A

Looks for named variables and functions first before going through script line by line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

function expression

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are IIFE?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is variable scope

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why do we prefer local variables to globals?

A

Reduces risk of naming conflicts

Use less memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write an object using literal notation

A
var  hotel = {
name: 'Quay',
rooms: 40,
booked: 25,
checkAvailibility: function() {
  return this.rooms - this.booked;
}
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we access an object’s properties

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a . (not a, but the . after it)

A

. is the member operator. it shows that property or method is related to an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are object constructers?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can I add a property to an object?

A

Use dot notation

hotel.spa = true;

17
Q

How do you delete a property/method from a object?

A

Use the delete keyword

delete hotel.booked;

18
Q

What does the this keyword do?

A

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

19
Q

How are arrays and objects connected?

A

Arrays are objects. You can store either in the other

20
Q

What built in objects are available on a webpage?

A

Browser Obkect Model; Document Object Model and Global Javascript Objects

21
Q

What is an Object Model?

A

An Object model is a group of objects, each of which represent related things from the real world. Together they form something larger.

22
Q

What does window.innerheight mean?

A

Browser object to model height of window (excluding browser chrome/interface)

23
Q

what does window.pageXOffset mean?

A

How far across the page the user has scrolled

24
Q

What does window.screenX mean?

A

x coordinate of the pointer relative to top left hand corner.

25
Q

What does window.screen.width mean?

A

Access width of screen in pixels

26
Q
What do these string methods do:
toUpperCase();
toLowerCase();
charAt();
indexOf();
lastIndexOf();
substring();
split();
trim();
replace();
A
  • 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).
27
Q

How can I check if something is a number?

A

isNan()

28
Q

What does toFixed() and toPrecision() do?

A

These methods return a string
toFixed: rounds to specific number of places
toPrecision: Rounds to total number of places

29
Q

What does toExponentiaL() do?

A

Returns a string to represent the number exponential notation . Javasgript can only model so many numbers with decimal points

30
Q

What do the following Math object methods do:

.round()
.sqrt(n)
.ceil()
.floor()
.random()
A
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)
31
Q

What formula can calculate a random number between 1 and 10?

A

Math.floor((Math.random()*10) + 1);

32
Q

How do you create a date with today’s current date and time?

A

Use the date constructor

var today = new Date();

33
Q

How are dates stored in javascript?

A

As a number. Milliseconds since 1st Jan, 1970

34
Q

What are three ways to set date and time?

A
new Date(1996, 11, 26, 15, 45, 55);
new Date('Dec 26, 1996 15:45:55')
new date(1996, 11, 26)
35
Q

How do you for a date:
get a date or set a date
get the Day
get year

A

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)

36
Q
How do you for a time:
get the hour
get millisecond
get minutes
get month
get seconds
get the time
A

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