Week 5 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?
Method definition is the function inside an object which has a name followed by a colon, while method call is when you call its method with arguments or without arguments
Describe method definition syntax (structure).
First you declare the object by creating a variable and equaling it to curly braces. Within the curly braces you have properties which can be given any name and then a colon. Following the colon you have a function. If you want to add more properties you have to add a comma at the end of the closing curly brace. If not, close the object with a closing curly brace followed by a semicolon.
Describe method call syntax (structure).
You call the object name followed by a period, then the name of the property followed by parentheses with arguments if it has any.
How is a method different from any other function?
A method is associated with a object and functions are not.
What is the defining characteristic of Object-Oriented Programming?
Objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
What is “abstraction”?
Being able to work with (possibly) complex things in simple ways.
What does API stand for?
Application Programming Interface
What is the purpose of an API?
The purpose of API is to communicate between apps with each other as intermediary, where both apps might have been built with different tools and technologies.
What is this in JavaScript?
It is an implicit parameter of all JavaScript functions. The value of this is determined by how a function is called.
What does it mean to say that this is an “implicit parameter”?
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.
When is the value of this determined in a function; call time or definition time?
Call time
What does this refer to in the following code snippet?
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
window until we call it then it will be the character object
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
It’s-a-me, Mario! Because our character object is responsible in calling the greet function.
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! The hello function is not being called as a method of an object so it defaults to the window object, which does not have this.firstName so it is undefined.
How can you tell what the value of this will be for a particular function or method definition?
You can until the function is called.
How can you tell what the value of this is for a particular function or method call?
Looks to the object to the left of the dot or it defaults to the global window object.
What kind of inheritance does the JavaScript programming language use?
Prototype-based inheritance or prototypal
What is a prototype in JavaScript?
Prototypes are the mechanism by which JavaScript objects inherit features from one another.
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?
Those methods are defined on a “prototype” object and simply borrow those methods when they are needed.
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
Looks in the object’s prototype chain
What does the new operator do?
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
It can create a new object with the formats of an object that has a constructor function.
What property of JavaScript functions can store shared behavior for instances created with new?
Prototype
What does the instanceof operator do?
It tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object and returns a boolean value.
What is a “callback” function?
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
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() method
How can you set up a function to be called repeatedly without using a loop?
setInterval() method
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
They both return a numeric ID value that identifies the timer created and can be used to cancel the timer.
What is a client?
The clients are the ones who request a service. They are either a laptop, computer, or smartphone
What is a server?
A computer that provides the network resources and provides service to other computers when they request it
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?
http, an http method (get, put, post), the absolute URL
What three things are on the start-line of an HTTP response message?
The protocol version (HTTP/1.1), a status code either 200, 404, 302, and a status text
What are HTTP headers?
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
Is a body required for a valid HTTP request or response message?
NO, it is a optional but it is commonly used.
What is AJAX?
AJAX allows you to collect data to update parts of the DOM of an HTML page without the need for a full page refresh.
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
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 event
Bonus Question: An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
Prototypal inheritance