Module 2 Flashcards
What is a method?
A method is a function which is a property of an object.
How can you tell the difference between a method definition and a method call?
Methods are defined within the object literal in a series of function definitions, a method is called as a property of the object called when optional parameters are included in the parenthesis
Describe method definition syntax (structure).
Methods are defined in the object literal using a method name : function (parameters) { code block for function definition }
or using the dot notation and assigning it a value
can see that there is a defined body to that method
Describe method call syntax (structure).
object.methodName(parameters);
How is a method different from any other function?
It is attached to an object and must be called as a method of the object that describes the behavior of the object (property of the object)
What is the defining characteristic of Object-Oriented Programming?
Objects contain both data (properties) and behavior (methods)
take related data and functionality and wrap them together in something called an object
What are the four “principles” of Object-Oriented Programming?
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
would add a fifth called XYZ
What is “abstraction”?
the most important part of the term “abstraction” boils down to being able to work with (possibly) complex things in simple ways.
wrapping complex tasks in simple interfaces
What does API stand for?
application programming interface
What is the purpose of an API?
the purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction
What is this in JavaScript?
an implicit parameter of all javascript functions
a JS keyword that references the current object you are in
What does it mean to say that this is an “implicit parameter”?
meaning that 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
implied parameter - changed based on how you call the method but it is implied bc you don’t actually list it out in the parameter list
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
It’s-a-me, Mario! because this refers to the character object
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
It’s-a-me, undefined!
because the function is not being called with the object this is referring to to the left of the dot
How can you tell what the value of this will be for a particular function or method definition?
You cannot, you can only tell the value of this when the function is called
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
It’s-a-me, undefined!
because the function is not being called with the object this is referring to to the left of the dot
because this then refers to the window, not the object
How can you tell what the value of this will be for a particular function or method definition?
You cannot, you can only tell the value of this when the function is called
How can you tell what the value of this is for a particular function or method call?
Look for an object to the left of the dot and if there is none, it will default to the window object
if not in the global scope, it will be undefined
What is a prototype in JavaScript?
an object that contains primarily methods and properties that can be used by other objects
What is a prototype in JavaScript?
an object that contains primarily methods and properties that can be used by other objects
property lookup on an object if the object doesn’t have it, it looks to that object’s prototype to see if that has the property
If an object does not have its own property or method by a given key, where does JavaScript look for it?
to the prototype object
If an object does not have its own property or method by a given key, where does JavaScript look for it?
to the prototype of the current object
What does the new operator do?
- Creates a blank, plain JavaScript object.
- Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
- Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
- Returns this if the function doesn’t return an object.
- Creates a blank object
- Sets that object’s prototype (__proto__) to equal the prototype property of the function it is called on (constructor)
- Runs the constructor function with ‘this’ that referring to new object
- Returns the return value of the function or the constructor function or if undefined, gives you back that new object
What property of JavaScript functions can store shared behavior for instances created with new?
__proto__
prototype property
What does the instanceof operator do?
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.
The instanceof operator tests the presence of constructor.prototype in object’s prototype chain.
Allows you to check if an object is an instance of a constructor
What is a “callback” function?
a function passed into another function as an argument
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?
setTimeout(callbackfunction, delayInMiliseconds)
How can you set up a function to be called repeatedly without using a loop?
setInterval(callbackfunction, delayInMiliseconds)
clearInterval(Integer that corresponds to your result value from setInterval call)
What is a client?
hardware or software that is requesting functionality or services
What is a server?
a hardware or software that provides functionality for other programs or devices
Which HTTP method does a browser issue to a web server when you visit a URL?
GET method
What three things are on the start-line of an HTTP request message?
- method
- target - specified by the URL (not the entire one, it’s the absolute path)
- version
What three things are on the start-line of an HTTP response message?
- version - of the http (1.1 or 1.0)
- status code
- status test
What are HTTP headers?
allow the client and the server to pass additional information
meta information / not the core data
extra contextual information (i.e. head tag of an HTML document)
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
i.e. content-type header can search on MDN
by convention, custom headers start with an X
Is a body required for a valid HTTP request or response message?
no, it is optional for both
httPie
tool for performing raw http requests (without a browser)
What is AJAX?
a technique for loading data into the page without having to refresh the page
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
(acronym for the technologies used in asynchronous requests)
XML = extensive markup language (extension of HTML where there is no limit for tag names, used as an interchange format, before JSON used for sending raw data back and forth)
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
there is an EventTarget prototype object that is the prototype of the XMLHttpRequest that has the addEventListener method
both DOM elements and XMLHttpRequest objects inherit the method from the same prototype (EventTarget) that does have that method
What is a code block? What are some examples of a code block?
a code block is denoted by {} and typically stems from a conditional statement i.e. if, if else, else, for, while, try
What does block scope mean?
the scope of the variable is local to the code block which it was initialized in
What is the scope of a variable declared with const or let?
block scope
What is the difference between let and const?
with const, variable is read-only reference to a value
can’t reassign the variable but can change the internals of it
Why is it possible to .push() a new value into a const variable that points to an Array?
because it is not directly redefining the array, since it is pointing to the array, you can manipulate it with the given methods .push & .pop but you cannot re-initialize the whole array variable to new values
How should you decide on which type of declaration to use?
depends on use case
What is the syntax for writing a template literal?
using back ticks
What is “string interpolation”?
substituting part of the string with a variable
concept, javascript template literal is an example of this
What is destructuring, conceptually?
destructuring allows you to assign properties of an object or elements of arrays to individual variables by flipping the literal on its head
can select multiple properties or indexes in the same statement
opposite of structuring - filling it with data
destructing is reverse - pulling data out of it
What is the syntax for Object destructuring?
let {property1: variable1, property2: variable2, property3:variable3} = Object
What is the syntax for Array destructuring?
let [variable1, variable2, variable3] = Array