Module 2 Flashcards
What is a method?
A function that is a property of an object.
How can you tell the difference between a method definition and a method call?
Function definition has structure function () { }, a call just has the function name and parameters, empty if none.
Describe method definition syntax (structure).
an object, function keyword, parenthese for arguments, and codeblock
Describe method call syntax (structure).
object.methodName()
How is a method different from any other function?
Stored as a property of an object
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (as properties) and behavior (as methods). You wrap functionality with the data structure (object).
What are the four “principles” of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism
What is “abstraction”?
Simplifying a complex action into a simple one
What does API stand for?
application programming interface
What is the purpose of an API?
A software interface that offers services to other software. A way for one software to use another software’s data without having to know how the other software works. Like going to a restaurant and ordering food, you don’t need to know how to make it or how to request the chef, just need to know what you want.
What is this in JavaScript?
An implicit parameter that changes depending on where in a function it is called. Usually refers to what object you are in.
What does it mean to say that this is an “implicit parameter”?
It is something available to use in a code block even if it was never explicitly declared or included in its parameter list.
When is the value of this determined in a function; call time or definition time?
Determined when the function is called.
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); } };
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 it calls the method in the object, which calls for a string plus the object firstName property with this.
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 ‘this’ refers to window since it is not directing to any object. Hello is only assigned the function greet, no connection to character object.
How can you tell what the value of this will be for a particular function or method definition?
Impossible to tell because this is defined by when it is called. You can make a best guess if it is a method.
How can you tell what the value of this is for a particular function or method call?
Find where the function is called and look for an object to the left of the dot.
What kind of inheritance does the JavaScript programming language use?
Prototypal (prototype-based). Objects inherit from other objects.
Trivia: Most other programming languages are class-based, they inherit from classes rather than objects.
What is a prototype in JavaScript?
Template that objects inherit properties and methods from
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
It has an existing prototype object in javascript
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
it checks the first prototype then checks the prototype of that prototype and so on, until it finds it, or returns undefined.
What does the new operator do?
It creates a new empty object then assigns that object’s prototype to be the prototype property of the constructor, then calls the constructor function assigning ‘this’, and if there is no return object for the function it uses ‘this’.
What property of JavaScript functions can store shared behavior for instances created with new?
Prototype property. Note that function objects have their own properties that can be found. 5 total properties, see MDN.
What does the instanceof operator do?
Checks if the prototype property of a constructor appears anywhere in the prototype chain of the object.
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() or setInterval().
Related ones: getAnimationFrame() and requestIdleCallback(), which adjust to how fast browser is running.
How can you set up a function to be called repeatedly without using a loop?
setInterval()
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
No delay. However, it is still asynchronous and anything without setTimeout() or setInterval() will run after anything that is not timed.
What do setTimeout() and setInterval() return?
an integer ID number which can be passed into clearTimeout()
What is a client?
The one that requests a service
What is a server?
Provider of resources or services to clients
Which HTTP method does a browser issue to a web server when you visit a URL?
GET method by default
What three things are on the start-line of an HTTP request message?
The method, path aka the request target, and the http method (protocol version).
Example format: PUT /create_page HTTP/1.1
What three things are on the start-line of an HTTP response message?
The http method (protocol version), status code (success or fail, 404 is a notable example), and status text description
What are HTTP headers?
They let the client or server pass additional information with a http request or response.
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN, developer.mozilla.org/en-US/docs/Web/HTTP/Headers
Is a body required for a valid HTTP request or response message?
No. Request typically does not contain body. Response does not necessarily always have a body.
What is AJAX?
A technique for loading data into part of a page without having to reload an entire page.
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
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
It inherited this object prototype property?
What is destructuring, conceptually?
Turning the properties of an object or values of an array into individual variables
What is the syntax for Object destructuring?
let { property1: variable1, property2: variable2 } = object;
What is the syntax for Array destructuring?
let [ arrayItem1, arrayItem2, …args ] = arrayName;
How can you tell the difference between destructuring and creating Object/Array literals?
destructuring is like a reverse of creating, appears like it is declaring the object or array first then assigning to a variable name.
What is the syntax for defining an arrow function?
(argument) => { argument function } or () => return or no parentheses for just one argument.
When an arrow function’s body is left without curly braces, what changes in its functionality?
The end result is returned automatically without a return statement.
How is the value of this determined within an arrow function?
Determined at definition, unlike function keyword which determines at function call.
What is Node.js?
Program that runs JavaScript outside of a browser, powered by v8 the same as Chrome. it is wrapped in various apis that allow it to interact with the host operating system. Browser JS is very limited, aka “sandboxed” to browser. It is an application server and can run programs that do not need to be in a web browser.
What can Node.js be used for?
To build back ends for web applications, command line programs, run web servers, write desktop applications, write mobile applications, or any other automation.
What is a REPL?
Read–eval–print loop. Also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user. Read means it interprets user input. Eval means it runs whatever the user inputs. Print means it returns a value, so it is undefined if nothing is specified. Loop means it can be done again afterwards.
When was Node.js created?
May 27 2009. Note how recent it is. But also this is long enough to be considered middle aged for a program.