Lecture 6: Advanced Javascript Flashcards

1
Q

What are functions?

A

Functions are reusable blocks of code
− are defined by the key word function
− have a unique function name
− can have parameters and/or return values
− need to be called to execute the code

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

Function syntax

A

function greet(person) {
let greeting = “Hello “ + person + “!”;
return greeting;
}
// calls the function once, store the return value
let greetingText = greet(“Venkat”);

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

Scope

A

What the function sees and has access to

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

Global Scope

A

accessible everywhere
Example: const x = 1 is declared outside of the function

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

Calling variables that the global scope cannot see

A

Reference Error

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

Events

A

functions that are called by Javascript on interactions

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

You define the function that should be executed

A

Event Handler / Callback function

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

And declare, when it should be called

A

Event Listener

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

Event Example

A

const container = document.querySelector(‘#container’);
container.addEventListener(‘click’, onClick);

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

Event Parameters

A

You must know what event types are available in Javascript
Example: Click
Resource: mdn web docs
(‘click’, onClick); = (event type, event handler)

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

Mouse Event Properties

A

Target: which HTML element threw the event
Button: which buttons were pressed
clientX / clientY: relative to current view/browser (generally useful)
screenX / screenY: relative to screen
pageX / pageY: relative to page, incl. Scrolled contents

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

Types of Events

A

The user selects, clicks, or hovers the cursor over a certain element <= common
The user chooses a key on the keyboard <= common
The user resizes or closes the browser window

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

Object

A

collection of related data and/or functionality

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

Object Literal

A

const objectName = {
member1Name: member1Value,
member2Name: member2Value,
member3Name: member3Value
};

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

Multiple objects

A

create a function that
creates and returns a new objec

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

Calling a constructor

A

− create a new object
− bind this to the new object, so you can refer to it
− run the code in the constructor
− return the new object.
Constructors, by convention, start with a capital letter and are named for the
type of object they create

17
Q

Constructor example

A

const salva = new Person(‘Salva’);
salva.name;
salva.introduceSelf();
//Hi! I’m Salva.

18
Q

This

A

this refers to the current object the code is being written inside
▪ in this case: this this is equivalent to Person
− enables you to use the same method definition for every object you create