4/11 - 4/15/2022 Flashcards

1
Q

What is a method?

A

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

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

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

A

The definition is in the ‘value’ position of an object, after the property. It starts with the ‘function’ keyword.
The method call is done by typing the object’s name followed by the property with dot notation.

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

Describe method definition syntax (structure).

A
cons obj = {
    firstName: Daniel,
    lastName: Cho,
    fullName: function () {
        return this.firstName + " " + this.lastName;
    }
};

The “function()” after “fullName” is an example of method definition syntax.

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

Describe method call syntax (structure).

A

Referring to last card, an example of a method call syntax would be obj.fullName();

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

How is a method different from any other function?

A

A function is a set of instructions that perform a task.

A method is a set of instructions that are associated with an object.

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

What is the defining characteristic of Object-Oriented Programming?

A

The defining characteristic of OOP is that objects can contain both data (as properties) and behavior (as methods).

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

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

A
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is “abstraction”?

A

Being able to work with complex things in simple ways.

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

What does API stand for?

A

Application Programming Interface

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

What is the purpose of an API?

A

To give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction.

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

What is ‘this’ in JavaScript?

A

‘this’ is an implicit parameter and its value is determined when the function is called. If the function is called by itself (not as a method of an object), the value of ‘this’ is the global window object. When the function is assigned as a method of an object, the value of ‘this’ is the object. If you cannot see where the function (or method) is called, you cannot say for sure what the value of ‘this’ is.

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

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

A

To say that ‘this’ is an ‘implicit parameter’ means 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’.

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

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

A

call time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
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);
  }
};
A

‘this’ refers to the object ‘character’. ‘this.firstName’ refers to ‘character.firstName’.

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

Given the above character object, what is the result of the following code snippet? Why?
character.greet();

A

‘It’s a me, Mario!’

Because ‘this’ is used in a function that is a method of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
A

‘It’s-a-me, undefined!’

This is the result of the code snippet because you cannot see where the method (character.greet) is being called.

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

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

A

You can’t. The value of ‘this’ is determined when the function or method is called, not when it’s defined.

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

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

A

You can tell what the value of ‘this’ will be for a particular function by checking to see if there is anything to the left of a dot preceding its name. If there isn’t anything, then the value of ‘this’ will be the global window object. For a method definition, the value of ‘this’ will be the object that is to the left of the dot.

19
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototype-based (or prototypal) inheritance

20
Q

What is a prototype in JavaScript?

A

A prototype in JavaScript is an object that has methods and properties passed into it so other objects can access the prototype to perform tasks that those objects cannot perform themselves.

21
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

The strings, arrays, and numbers borrow those methods from their respective prototype objects where those methods are defined.

22
Q

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

A

In its prototype chain

23
Q

What does the ‘new’ operator do?

A

It creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

Per MDN:

  1. Creates a blank, plain JavaScript object.
  2. Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
  3. 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).
  4. Returns ‘this’ if the function doesn’t return an object.
24
Q

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

A

Function.prototype

Ex: function Car() {}
car1 = new Car();
car2 = new Car();

console.log(car1.color); // undefined

Car.prototype.color = ‘original color’;
console.log(car1.color); // ‘original color’

car1. color = ‘black’;
console. log(car1.color); // ‘black’

console. log(Object.getPrototypeOf(car1).color); // ‘original color’
console. log(Object.getPrototypeOf(car2).color); // ‘original color’
console. log(car1.color); // ‘black’
console. log(car2.color); // ‘original color’

25
Q

What does the ‘instanceof’ operator do?

A

‘instanceof’ tests to see if the ‘prototype’ property of a constructor appears anywhere in the prototype chain of an object.

Ex: function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car);
// expected output: true
console.log(auto instanceof Object);
// expected output: true
Ex.2: // defining constructors
function C() {}
function D() {}

let o = new C()

// true, because: Object.getPrototypeOf(o) === C.prototype
o instanceof C
// false, because D.prototype is nowhere in o's prototype chain
o instanceof D

o instanceof Object // true, because:
C.prototype instanceof Object // true

C.prototype = {}
let o2 = new C()

o2 instanceof C // true

// false, because C.prototype is nowhere in
// o's prototype chain anymore
o instanceof C
D.prototype = new C()  // add C to [[Prototype]] linkage of D
let o3 = new D()
o3 instanceof D        // true
o3 instanceof C        // true since C.prototype is now in o3's prototype chain
26
Q

What is a client?

A

The service requester in the client-server model. The client requests services from the server, such as getting resources from the server or pushing data to the server

27
Q

What is a server?

A

Providers of a resource or services in the client-server model. A couple of the forms a server can take is a web server or a file server.

28
Q

Which HTTP method does a browser issue to a web server when you visit a URL?

A

An HTTP GET request

29
Q

What three things are on the start-line of an HTTP request message?

A
  1. An HTTP method (GET, PUT, or POST)
  2. The request target, usually a URL or the absolute path of the protocol, port, and domain are usually characterized by the request context.
  3. The HTTP version
30
Q

What three things are on the start-line of an HTTP response message?

A
  1. The protocol version, usually HTTP/1.1
  2. A status code, indicating success or failure of the request. Common status codes are 200, 404, or 302.
  3. A status text. A brief, purely informational, textual description of the status code to help a human understand the HTTP message.
31
Q

What are HTTP headers?

A

HTTP headers are one line of code messages that allow the client and server to send additional information to each other.

Headers allow you to add more information for a request.

32
Q

Where would you go if you wanted to learn more about a specific HTTP Header?

A

MDN

33
Q

Is a body required for a valid HTTP request or response message?

A

No

34
Q

What is a “callback” function?

A

A callback function is a function that passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

35
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

The global setTimeout() function

36
Q

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

A

The setInterval() function

37
Q

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

A

0 ms

38
Q

What do setTimeout() and setInterval() return?

A

A numeric, non-zero value which identifies the timer created by the call to setTimout() or setInterval().

The id that is used to cancel the interval or timeout.

39
Q

What is AJAX?

A

AJAX is a technique for loading data into part of a page without having to refresh the entire page.

40
Q

What does the AJAX acronym stand for?

A

Asynchronous JavaScript and XML

41
Q

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

A

The XMLHttpRequest constructor

42
Q

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

A

The ‘load’ event

43
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

They are both part of the eventarget interface (i.e. inheritance).

And also because of the Object constructor