JavaScript Flashcards

1
Q

• What is the purpose of variables?

A

o Variables create permanence in your data

o It creates a box and stores information

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

• How do you declare a variable?

A

o Use a keyword like let or var.

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

• How do you initialize (assign a value to) a variable?

A

o Use an assignment operator (SINGLE EQUAL SIGN “=”)

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

• What characters are allowed in variable names?

A

o _ underscore and $. BUT variable names CANNOT start with a numeric value.

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

• What does it mean to say that variable names are “case sensitive”?

A

o String and string are different.

o It is not good practice to use variable names that are the same but with different casing.

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

• What is the purpose of a string?

A

o JavaScript strings are for storing and manipulating TEXT.
o These character are written INSIDE quotes.
o The string object is used to represent and manipulate a sequence of characters

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

• What is the purpose of a number?

A

o To store numeric values FOR calculations – mathematical operations

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

• What is the purpose of a boolean?

A

o To express a value of True or false
o Kind of like a light switch –
o Necessary for conditions and comparisons
o Helpful to determine whether or not to execute or not. (ON/OFF | it IS or IS NOT)
o Purpose of Booleans is to make DECISIONS (Yes or No)

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

• What does the = operator mean in JavaScript?

A

o Assigner

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

• How do you update the value of a variable?

A

o Use the assignment operator and WOULD NOT USE the keyword.
o VAR keyword is only necessary when creating the variable for the first time.
o CONFUSING.
o Does this variable already exist in my code?

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

• What is the difference between null and undefined?

A

o Null is assigned
 Null is a value that can only exist because someone has assigned it to something.
 It is purposeful – since someone intentionally put it there
o Undefined
 Undefined is organic.
 If there is no return value, it will come back as undefined.
 Undefined is assigned by the browser – the ONLY value that JS can use to say “nothing”

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

• Why is it a good habit to include “labels” when you log values to the browser console?

A

o For clarity so that when we return to the code, we can see what we were working on and not get confused.
o Labels makes it clear what we were working with.

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

• Give five examples of JavaScript primitives.

A

o String, number, Boolean, null, undefined.

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

• What data type is returned by an arithmetic operation?

A

o Numbers – not necessarily integers.

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

• What is string concatenation?

A

o Adding together multiple strings.
o Concatenation will NEVER change the original string.
o Strings are immutable.
o They can never, EVER change.

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

• What purpose(s) does the + plus operator serve in JavaScript?

A

o Summing numerical data or string concatenation.

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

• What data type is returned by comparing two values (<, >, ===, etc)?

A

o Boolean

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

• What does the += “plus-equals” operator do?

A

o Shorthand for reassigning a value of the original variable to the new value.
o =+ will make a lasting change to something while + will add but will not save the previous value.

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

• What are objects used for?

A

o Objects group together a set of variables and functions to create a model of something you would recognize from the real world.

o In an object, variables and functions take on new names.

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

• What are object properties?

A

o In an object, variables become known as properties.
o If a variable is part of an object, it is called a PROPERTY.
o Properties tell us about the object, such as the name of a hotel or the number of rooms it has.
o Each individual hotel might have a different name and a different number of rooms.

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

• Describe object literal notation.

A

o THEY ARE A SET OF CURLY BRACES
o Properties separated by a colon.
o Commas to separate lines.

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

• How do you remove a property from an object?

A

To delete a property, use the delete keyword followed by the object name and property name.
ex: delete hotel.name;

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

• What are the two ways to get or update the value of a property?

A

o Use a dot or the brackets

o Bracket notation might be used when a property is inside an existing object.

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

• What are arrays used for?

A

o Allows us to group data types (typically alike – but can be different) and gives us a way to put it into a list format and helps us deal with them one item at a time and see the scope of each item.

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

• Describe array literal notation.

A

o Enclosed in square brackets [ ] – separated by commas.

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

• How are arrays different from “plain” objects?

A

o Arrays do not have individually named pieces of data – BUT are ordered sequentially numerically indexed.
o Arrays have an order and tell us how many properties are inside. Objects do NOT.
o To add data to an array, you use a method named PUSH.
o To add data to a property of an object, use the assignment operator.
o Arrays show and tell us how many things we are working with.

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

• What number represents the first index of an array?

A

o 0 ZERO

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

• What is the length property of an array?

A

o Property in all arrays

o Returns a true count of how many items are in that array.

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

• How do you calculate the last index of an array?

A

o Write out the length property of the length object.

o Whatever the value is of the array object and subtract 1.

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

• What is a function in JavaScript?

A

o A block of code

o A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value

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

• Describe the parts of a function definition.

A

o Function keyword, optional name, parameter list (with optional parameters), code block, return

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

• Describe the parts of a function call.

A

o Write the name of the function, parenthesis, and any number of arguments.
o What if there are no parameters?!
 EMPTY PARENTHESIS.
 A function call at its most basic form is the name and empty parenthesis.
 Without parenthesis, NOTHING happens.

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

• When comparing them side-by-side, what are the differences between a function call and a function definition?

A

o Function call has the function name and argument.

o Function definition has function keyword – otherwise, it is a function call.

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

• What is the difference between a parameter and an argument?

A

o Parameter is a placeholder that just stays there.

o Argument, actual value that takes space in the placeholder.

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

• Why are function parameters useful?

A

o Without parameters, you’d have to create a new function for each use.
o Parameters provide mutability.

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

• What two effects does a return statement have on the behavior of a function?

A

o Causes the function to produce a value
o Prevents any more code in the function’s code block from being run. AKA – IT STOPS THE FUNCTION ENTIRELY!
o Functions that are meant to do a job won’t have a return BUT Functions that are meant to have a value WILL HAVE a return.

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

• Why do we log things to the console?

A

o A window we can communicate with the code –
a debugging tool

o Log stuff out and see what values are being used!

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

• What is a method?

A

• A method is a function which is a property of an object.
There are two kind of methods: Instance Methods which are built-in tasks performed by an object instance, or
Static Methods which are tasks that are called directly on an object constructor.

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

• How is a method different from any other function?

A

o A function is a block of code

o A method requires you to specify what object that function is attached to USING DOT NOTATION. (library.push(js))

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

• How do you remove the last element from an array?

A

o pop() method.

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

• How do you round a number down to the nearest integer?

A

o Floor method of the Math object. –> math.floor

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

• How do you generate a random number?

A

o Math.random

(range 0 to 1… not including 1 – so, .9999999999)

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

• How do you delete an element from an array?

A

o Math.splice

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

• How do you append an element to an array?

A

o Push method

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

• How do you break a string up into an array?

A

o Split method – give it a set of character and it will look for those character and chop them out.

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

• Do string methods change the original string?

How would you check if you weren’t sure?

A

o It does not. No string methods EVER change the original string – they are IMMUTABLE!
o If not sure, you can console.log the method of the string.
o Log out the string and see if it changes!

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

• Roughly how many string methods are there according to the MDN Web docs?

A

o Many many.

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

• Is the return value of a function or method useful in every situation?

A

o a return value is used where the function is an intermediate step in a calculation of some kind. You want to get to a final result, which involves some values that need to be calculated by a function. After the function calculates the value, it can return the result so it can be stored in a variable; and you can use this variable in the next stage of the calculation.

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

• Roughly how many array methods are there according to the MDN Web docs?

A

o Many many.

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

• What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?

A

o MDN

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

• Give 6 examples of comparison operators.

A

o Simply equal to

o NEVER USE = =

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

• What data type do comparison expressions evaluate to?

A

o Booleans

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

• What is the purpose of an if statement?

A

o To determine whether to run a code block and check a condition

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

• Is else required in order to use an if statement?

A

o Nope

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

• Describe the syntax (structure) of an if statement.

A

o If keyword with the condition which is evaluated to true/false
o If keyword, condition, code block.

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

• What are the three logical operators?

A

o And &&
o Or ||
o Not !

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

• How do you compare two different expressions in the same condition?

A

o Use AND / OR && or ||

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

• What is the purpose of a loop?

A

o Loops offer us the ability to repeat a single step / an action

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

• What is the purpose of a condition expression in a loop?

A

o To evaluate when to stop

o In the absence of a condition, loops will go on forever.

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

• What does “iteration” mean in the context of loops?

A

o One time that the code block gets run.

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

• When does the condition expression of a while loop get evaluated?

A

o At the beginning before each iteration

(BEFORE the code block runs)

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

• When does the initialization expression of a for loop get evaluated?

A

o The initialization step occurs one time only,
before the loop begins

o BEFORE ANYTHING – the very first thing.

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

• When does the condition expression of a for loop get evaluated?

A

o First time, after the initialization
o Every subsequent time, before the code block of the next iteration
 And after the final expression or the initialization

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

• When does the final expression of a for loop get evaluated?

A

o Before the condition and AFTER the code block.

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

• Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?

A

o Break.

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

• What does the ++ increment operator do?

A

o Adds AND saves one to the variable

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

• How do you iterate through the keys of an object?

A

o With a for – in loop

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

• Why do we log things to the console?

A

o To ensure what your code is doing what you want it to do.

69
Q

• What is a “model”?

A

o A representation of something.

70
Q

• Which “document” is being referred to in the phrase Document Object Model?

A

o The HTML document

71
Q

• What is the word “object” referring to in the phrase Document Object Model?

A

o Data type that can refer to
o DOM is a javascript object that is modeling the html document’s content.

o The Document Object Model (DOM) is the data representation of the objects that comprise the structure and content of a document on the web.

72
Q

• What is a DOM Tree?

A

o All of the information necessary to convey ONE element and all of its content.
o All of the necessary data to make an element.

73
Q

• Give two examples of document methods that retrieve a single element from the DOM.

A

o getElementByID and querySelector
o the only method you need to retrieve a single element is QUERYSELECTOR.
o The reason is, querySelector can find anything you are looking for.

74
Q

• Give one example of a document method that retrieves multiple elements from the DOM at once.

A

o querySelectorAll

75
Q

• Why might you want to assign the return value of a DOM query to a variable?

A

o So you don’t have to continue calling the result over and over again.
o Get it once and hold onto it.

76
Q

• What console method allows you to inspect the properties of a DOM element object?

A

o console.dir
o logged elements will look like the html code.
o If you need to look closer, use dir and it will show you the actual details that element is associated with.

77
Q

• Why would a tag need to be placed at the bottom of the HTML content instead of at the top?

A

o You want elements to exist BEFORE you look for them.
o LOAD ORDER (html runs from top to bottom).
o All of the body info must load in FULL before looking for those elements.

78
Q

• What does document.querySelector() take as its argument and what does it return?

A

o Takes a string containing a CSS selector and it only returns that which matches that selector.

79
Q

• What does document.querySelectorAll() take as its argument and what does it return?

A

o Takes the string and returns all of the matching elements.

80
Q

• Why do we log things to the console?

A

o To check the functionality and accuracy of our code

81
Q

• What is the purpose of events and event handling?

A

o Event handling – allows the developer to do something with the code – INTERACTIVITY.

82
Q

• Are all possible parameters required to use a JavaScript method or function?

A

o No – some are optional (LIKE BOOLEANS)

83
Q

• What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

o addEventListener – allows us to create a functionality that will occur when users click on an event.

84
Q

• What is a callback function?

A

o A function passed as an argument to another function. And that other function can use that function if it needs to

85
Q

• What object is passed into an event listener callback when the event fires?

A

o Event object (Page 246-247 in Duckett)
o Javascript is aware that this is a problem so what it does, when an event occurs, in that exact micro second, it creates an object from scratch and in that object there is all the relevant data – that way inside the handler definition, you can dig into that object and get specific information about that occurrence.

86
Q

• What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A

o Where the event originated
 console.log – log it out and inspect
 MDN – read the documentation.

87
Q

• What is the difference between these two snippets of code?

element. addEventListener('click', handleClick)
element. addEventListener('click', handleClick())
A

o The first one will execute when the task is triggered (e.g when the item is clicked)
o The second one will return an error

88
Q

• What is the className property of element objects?

A

o The className property of the Element interface gets and sets the value of the class attribute of the specified element

89
Q

• How do you update the CSS class attribute of an element using JavaScript?

A

o className property

o You need the DOM element dot notation and className (camelCase) assigned to the new object.

90
Q

• What is the textContent property of element objects?

A

o The textContent property returns:
The text content of the element and all descendants, with spacing and CSS hidden text, but without tags.
o All the text hidden or not.

91
Q

• How do you update the text within an element using JavaScript?

A

o Query the element somehow and then…

o textContent property to access it by assignment

92
Q

• Is the event parameter of an event listener callback always useful?

A

o Not always – vast majority of the time, it is.

93
Q

• Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A

o Variable needs to exist outside of the function block

o Query select the element that increases the click count

94
Q

• Why is storing information about a program in variables better than only storing it in the DOM?

A

o You want the data you use to be present in JS.

o It’s better to use data within the same language you are in rather than depending on other languages.

95
Q

What do we actually do to get data from a form?

A

We set up our own code to take data out of the form and work with it.

This helps to validate and use it before sending it to wherever we are sending it.
It is control.

96
Q

• What event is fired when a user places their cursor in a form control?

A

o Focus

97
Q

• What event is fired when a user’s cursor leaves a form control?

A

o Blur

98
Q

• What event is fired as a user changes the value of a form control?

A

o Input

o JavaScript INPUT vs. JavaScript CHANGE EVENT – THEY ARE DIFFERENT!

99
Q

• What event is fired when a user clicks the “submit” button within a ?

A

o Submit

100
Q

• What does the event.preventDefault() method do?

A

o It prevents the default action
o Make it your FIRST line of code IN the function.
o The default action should NOT be taken as it normally would.
 Example: Checkboxes, that by default have a check – would not be marked.
o event.preventDefault() is ALWAYS present with a Submit button.
 Otherwise, it will reload.

101
Q

• What does submitting a form without event.preventDefault() do?

A

o It will simply RELOAD the page.

102
Q

• What property of a form element object contains all of the form’s controls?

A

o Every dom element has a property named ELEMENTS and that Elements property contains all of the form controls.

103
Q

• What property of a form control object gets and sets its value?

A

o The Value Property in the Form Control gives you the value.

104
Q

• What is one risk of writing a lot of code without checking to see if it works so far?

A

o It can be broken – amongst a variety of other things.

105
Q

• What is an advantage of having your console open when writing a JavaScript program?

A

o You can see the small push forward and progress with the development of the page.
o Keep track of code and logs and errors in real time.

106
Q

• Does the document.createElement() method insert a new element into the page?

A

o Not right away… not ever.

o By calling the createElement element, it will create it, but it will not be visible to the user.

107
Q

• How do you add an element as a child to another element?

A

o appendChild method.

108
Q

• What do you pass as the arguments to the element.setAttribute() method?

A

o Html does not have data types so no need to preserve data types.

109
Q

• What steps do you need to take in order to insert a new element into the page?

A

o Create the element, give some content, insert to the page (appendChildmethod),
o If we do not have a variable already holding that element, use a querySelector (seek out and find the element)
o With newly made elements, we have immediate access because you created it… but for existing elements, you have to look for it.
o Query the element, actually make the element you want to put in the dom, configure it with any necessary information, and call it.

110
Q

• What is the textContent property of an element object for?

A

o appendText to node….

o Give it a new piece of content or observe a new piece.??

111
Q

• Name two ways to set the class attribute of a DOM element.

A
o	setAttribute with string class
o	assign a new value to the property.
112
Q

• What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?

A

o You can reuse it.
o You can call that function again to make the same structure multiple times
o Utilize for multiple contexts.
o Use more than once, yes, but ALSO in multiple situations.

113
Q

• What is the event.target?

A

o Property on the event object that is a reference where the event was fired from

114
Q

• Why is it possible to listen for events on one element that actually happen its descendent elements?

A

o Event bubbling

115
Q

• What DOM element property tells you what type of element it is?

A

o Tag name

o Could be node name – but no.

116
Q

• What does the element.closest() method take as its argument and what does it return?

A

o Argument is the CSS selector
o Closest ancestor.
o Closest is LITERALLY the opposite of the querySelector – querySelector only looks inward whilst closest works outward.
 TWIST: every dom element has a querySelector-
 So in this exercise there is a $tasklist that is holding the element, UL

117
Q

• How can you remove an element from the DOM?

A

o Use remove method (remove()) on the object or the dom element you want to remove.

118
Q

• If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?

A

o Add it to its parent.

119
Q

• What is the event.target?

A

o A property on the event object which stores an element of the dom object to where the event originated from.

120
Q

• What is the affect of setting an element to display: none?

A

o Removes that from the document flow = it is no longer visible.

121
Q

• What does the element.matches() method take as an argument and what does it return?

A

o It takes the CSS selector as a string and returns a Boolean that represents matches the selector as an argument

122
Q

• How can you retrieve the value of an element’s attribute?

A

o Call the getAttribute

o Argument will be a string which is the name of the attribute we want to retrieve.

123
Q

• At what steps of the solution would it be helpful to log things to the console?

A
o	Any time you want to store it to a variable and check what you’re using.
o	Call a method
o	Comparison expressions
o	Variables you assign values to.
o	EVERY TIME YOU DO ANYTHING.
124
Q

• If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?

A

o Add an eventListener for individual actions.

o Doing that would be code that is not scalable nor would it grow nicely.

125
Q

• If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?

A

o A long list of if statements (a HUGE CONDITIONAL Block – again, not scalable).

126
Q

• What is JSON?

A

o Javascript object notation – a format for rendering data that follows the __ of js objects

127
Q

• What are serialization and deserialization?

A

o Serialization uses data where all of the data is in one continuous string – pieces of data lined up right next to each other.
o Deserialization takes the data that is one continuous string and reverts it back to its more complex state to make things easy to access.
 Think about Cody’s illulstration on packing for a trip and unpacking when you get back… from multiple locations to your bag… then back to their respective locations.

128
Q

• Why are serialization and deserialization useful?

A

o When you store data, you want an easy way to store it.

129
Q

• How do you serialize a data structure into a JSON string using JavaScript?

A

o Use the stringify method and pass the json

130
Q

• How do you deserialize a JSON string into a data structure using JavaScript?

A

o Use the parse method

131
Q

• How do you store data in localStorage?

A

o Setitem method of the local storage object

132
Q

• How do you retrieve data from localStorage?

A

o Getdata method of the local storage object

133
Q

• What data type can localStorage save in the browser?

A

o JUST STRINGS

134
Q

• When does the ‘beforeunload’ event fire on the window object?

A

o Right before you close.
o When the window document and resources are about to close
o (close/refresh/close the browser/crashes)

135
Q

• What is a method?

A

o A method is a function which is a property of an object.

136
Q

• How can you tell the difference between a method definition and a method call?

A

o Method definition will have the function code block including keyword and code block
o Method call will ….

137
Q

• Describe method definition syntax (structure).

A

o Object literal and send it to a variable
o Property name WITH Colon
o Function definition with or without a name (use the name of the property)

138
Q

• Describe method call syntax (structure).

A

o object.methodName(arguments)

139
Q

• How is a method different from any other function?

A

o Method is associated with an OBJECT

140
Q

• What is the defining characteristic of Object-Oriented Programming?

A

141
Q

• What are the four “principles” of Object-Oriented Programming?

A

142
Q

• What is “abstraction”?

A

o Process of reducing complexity to the bits the user needs to know.

143
Q

• What does API stand for?

A

o Application Programming Interface

144
Q

• What is the purpose of an API?

A

o To hide the internal details of how a system is architected.
o Gives us a more simplistic way to interact with a more complex system.
o Light switch…
o DOM is an API – it is not part of the JavaScript language … it was built in the periphery – a complex system that has access to the complexities of the page. Very complicated but we can simply use it.

145
Q

• What is ‘this’ in JavaScript?

A

o ‘this’ is different from explicit parameters

o implicit parameter to see where the object was invoked

146
Q

• What does it mean to say that this is an “implicit parameter”?

A

o it is available in a function’s code block even though it was never included in the function’s parameter list or declared with var.

147
Q

• When is the value of this determined in a function; call time or definition time?

A

o Call time

148
Q

• What does this refer to in the following code snippet?

A
var character = {
  firstName: 'Mario',
  greet: function () {
    var message = 'It\'s-a-me, ' + this.firstName + '!';
    console.log(message);
  }
};
o	NOTHING – it is NOT being called.
o	Implicit parameter – very important to understand.
149
Q

• Given the above character object, what is the result of the following code snippet? Why?

A

character.greet();

o It’s-a-me, Mario!

150
Q

• Given the above character object, what is the result of the following code snippet? Why?

A
var hello = character.greet;
hello();
o	It’s-a-me, undefined!
151
Q

• How can you tell what the value of this will be for a particular function or method definition?

A

o Within a definition, you can’t guarantee that it will have any specific value. It’s a place holder for a future value.

152
Q

• How can you tell what the value of this is for a particular function or method call?

A

o Whatever is to the left of the dot… i.e. window.hello();

o Look to the left of the dot when the function is called as a method.

153
Q

• What kind of inheritance does the JavaScript programming language use?

A

o Prototypal inheritance in JavaScript a fairly simple concept (especially compared to this). The ultimate goal is to move properties and methods that we’d like to reuse into a “prototype” object and then tell other objects to simply delegate (verb) to that “prototype” object when they aren’t able to perform the required tasks themselves.

154
Q

• What is a prototype in JavaScript?

A

o a JavaScript prototype is simply an object that contains properties and (predominantly) methods that can be used by other objects.
o The prototype is responsible for being the focal point of the behavior that needs to be in one spot for all other methods to use.

155
Q

• How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?

A

o Because all of those have a prototype and have a shared behavior used/borrowed for later on.

156
Q

• If an object does not have its own property or method by a given key, where does JavaScript look for it?

A

o In the prototype object
o If there are three or four layers of prototype, it will begin at the nearest parent to the next and to the next. (prototypal chain)

157
Q

• What does the new operator do? READ MDN

A

o It allows us to invoke a function as an constructor function.
o THIS MEANS: It makes an empty object and it will take the __proto__ property and assigns it to the prototype function
 Points the
o 3 –
o 4 – It returns the object if no other is specified.
 In a constructor function, there is no return value (< 0.000001%).
 By not having a return, you get the default, which is to return the object you’ve created.

158
Q

• What property of JavaScript functions can store shared behavior for instances created with new?

A

o Prototype property

o Every function EVER has the prototype property and it is an object.

159
Q

• What does the instanceof operator do?

A

o It checks

o OBJECT –> instanceof –> CLASS

160
Q

• What is a “callback” function?

A

161
Q

• Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?

A

o setTimeout()

162
Q

• How can you set up a function to be called repeatedly without using a loop?

A

o setInterval

163
Q

• What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?

A

o Default time delay of zero.

164
Q

• What do setTimeout() and setInterval() return?

A

o setTimeout returns…
o setInterval returns…
o They both share the same ID pool.

165
Q

• What is AJAX?

A

o Ajax is a technique for loading data into part of a page without having to refresh the entire page.
o (Data is often sent in a format called JavaScript Object Notation (or JSON)

166
Q

• What does the AJAX acronym stand for?

A

o Asynchronous JavaScript and XML

o However, Ajax is no used to refer to a group of technologies that offer asynchronous functionality in the browser.

167
Q

• Which object is built into the browser for making HTTP requests in JavaScript?

A

o XMLHttpRequest

168
Q

• What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?

A

o Load

169
Q

• Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?

A

o They share a prototype object.