Exam checklist Flashcards

1
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:

Object Oriented Programming

A

OOP is thinking about and organizing code in terms of objects. For example bundling state and behaviour onto objects. Have the data tha tdescribes an object and the operations that aact upon it in the same object.
This makes it easier to think about and maintain.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

object factories,
Advantages and disadvantages vs constructors.

A

An object factory is a function that creates and returns an object. It can be used to create objects in bulk.

Advantages vs constructors
Can return any arbitrary and highly customizable object. Arrays, functions.

If inheritance isn’t needed, it’s a simpler way to return an object.
Object factories bypass any confusion with “this”.

Disadvantages vs constructors
No built in way to know what factory created what object.

Takes up more memory. All objects have all properties and methods. A factory that creates an object with the function speak(), every single object will have that identical function.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

constructors

A

A constructor is a function that, like factories, can create and return objects used to create objects in bulk of a certain type.

But there’s not necessarily anything unique about constructor function. Any function can be called as a constructor using the new keyword which executes a specific constructor algorithm that will return an object at the end.

They are different from factories in that they automatically set the prototype of the objects it creates to the prototype property on the constructor function.
Also objects created by constructors are said to be instances of the constructor. And can be verified to be instances with instanceof.

There’s a specific algorithm when you run the new keyword.
1. creates an object
2. sets this to refer to that object
3. sets the prototype of the new object to the prototype property on the function
4. executes the function body
5. returns the new object

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

prototypes

A

It’s a sort of relationship between objects. One object can be a prototype of another.
If you try to access a property on an object, but that object doesn’t have it, it will delegate that call to the prototype. If that object doesn’t have it either, it will delegate the call to it’s prototype if it has one and so on until the null object.

For example:
Video game
instead of having
Enemy type A with hp, and then a method reduce hp
Enemy type b with hp, and then a method reduce hp
They each have a prototype enemy, with the method reduce hp on it.

This way you don’t have to have multiple identical functions. This taking up less memory space and makes it easier to maintain.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

OLOO

A

Objects linking to other objects is sort of a prototypal inheritence pattern, which also has an object creation pattern.

So in objects lending to other objects, you get a prototypal chain that is just object to object.
WIth constructors the prototypal chain is prototype property to prototype property on a class or constructor.

To create objects in OLOO, you have a method on one of the objects that sets the properties for a given object.

With constructors, it’s a function that creates an object, with a object property that is the prototype. WIth OLOO the object itself is the prototype and it has a function property that creates the object.

OLOO benefits over factories is:
- uses prototype, same benefit as constsructors in saving memory space

Some people prefer OLOO because it embraces JavaScript’s quirkiness and can save some lines of code. The drawback is the loss of instanceof for quick inheritance checks.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

ES6 classes

A

Other languages have classes. JS doesn’t. But it employs a snytactic sugar to resemble them, but it’s just constructors. So they have a unique syntax but this syntax basically sets up a constructor function.

They are very similiar to constructors except they:
- use strict within
- have to be called with new keyword
- not hoisted
- the prototype cannot be reassigned

JS classes/constructors cannot have 2 prototypes like some languages.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Prototypal and pseudo-classical inheritance

A

Prototypal inheritance is inheritance object to object.

Pseudo classical inheritance is when the prototype is an object on a constructor

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Methods and properties; instance and static methods and properties

A

Methods and properties are values stored on object accessible via keys.
Instance methods and instance properties are acessible via instances. If you try to call them from the class or constructor you can’t (unless if you go to the prototype). We usually refer to methods on the prototype as instance methods too.

Whereas static properties are those accessed on the class or constructor, not on an instance or prototype. They belong to the type.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Encapsulation

A

The idea of bundling or combining the data (or state) and the operations (or behaviour) that work on that data into a single entity, e.g., an object.
This makes it easier to organize and maintain.

So if you have a video game. You don’t have the character object with an axe, and that axe has a method that reduces the hp property of enemy objects. The enemy object has an HP property, and it also has a method to reduce it’s own hp. This way it’s easier to maintain, you don’t have to hunt for this method that effects this object. Or hunt for these enemies that are affected by this axe method.

Objects can interface with one another. But it’s more along the line of if axe strikes enemy, operate the enemy.decreaseSelfHp() method. And you can start to take advantage of polymorphism. Each enemy can have a difference execution of decreaseSelfHp. Rather than having these methods on the axe that check to see the type of enemy.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Polymorphism

A

Polymorphism is about different objects of different types to respond similarly to the same message or method invocations.

Nice examples
.includes() in a string or an array. Completely different methods. They are different methods on different objects, but they have the same name, and similar functionality so they are named the same.
Same with .toString( ). You just want it to turn into a string, you don’t care how or what object you’re calling it from.

Those are examples of ducktyping, where completely different objects have a method of the same name.

There is also polymorphism by prototype. Where in a prototypal chain there are methods of the same name. Example could be an enemy object in a game, slime, which is the prototype for adult slime. Both adult slime and slime, on death wil split into smaller slimes. Adult slime has a split method, turns it into 2 slimes. But we want those slimes to have another split mehod, that turns them into baby slimes. Same .split name, similar purpose, but different executions and results.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Collaborator objects

A

A collaborator object helps provide state to another object. An example is, you have an owner object, and a cat object. This owner owns the cat. Rather than trying to remember that, the owner object has a pet property that references that cat object.

This helps organize and encapsulate things. May this owner has a feed pets method. Now that method doesn’t have to reference a specific object or keep strings of pet names where you have to make sure these pet names are different or something. It can reference the pet objects directily within this.pets to see what needs feeding. You don’t have to store strings of it’s pets and then access those pets on a animals object or something, they’re just access directly as collaborators.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Single vs multiple inheritance

A

In some languages, like C++ and python, an object can inherit from multiple classes.
In JS there’s only one. So in JS to get around this, we are forced to kind of take almost an object factory approach, where we put multiple methods or properties on some objects but not others depending on whether we need it.
We call them mix-ins.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Mix-ins; mix-ins vs. inheritance

A

Mix-ins is how we get around not having multiple inheritance in JS, which doesn’t have inheritance from multiple classes.

Basically if want a cormorant bird object to inherit from both a flying bird class, and a swimming bird class, we can’t. So we have it inherit from bird class. And then bestow, using making Object.assign, the methods or properties of a swimming birds, and flying birds.

Or it inherits from a flying bird, then we bestow it with properties of swimming birds.

That sort of thing. It has the disadvantages of factory functions, we’re adding identical code onto multiple objects. But using an object as a blueprint and assigning it to the objects we want, it’s still manageable.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Methods and functions; method invocation vs. function invocation

A

A method is just a function referenced by an object. The distinction isn’t super significant, except in terms of the execution context.

The execution for what we have learned so far, mostly affects what the this keyword refers to.

If its just a global function, it will be implicitly set to reference the global object, in node. Here it might be undefined.

A method implicitly sets the execution context to the object that called the method. When we go you know, randomObject.hasOwnProperty(‘aProperty’)

Even though hasOwnProperty isn’t on randomObject, it was called by it. And this will be set to randomObject.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:

Higher-order functions

A

A higher order function takes one or more function objects as arguments and/or returns a function object as its result.

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

Can you explain, to someone who doesn’t know about it and give any useful examples:
The global object

A

JavaScript creates a global object when it starts running. A global function will operate with the global object as its implicit context.

It has several built in properties and methods. Like Infinity and Nan, isNaN, parseInt.

If you assign a value to a variable without let, const, or var, it creates a property on global.

17
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:
Method and property lookup sequence

A

First, it looks for the property on the object that was it was requested on. If the property is not there, it delegates the request to its prototype. If it’s not there, it delegates it to that object’s prototype. JS searches the prototype chain to find the method.

If a property of the same name is on multiple objects in the prototype chain, it takes the one closest to the original request.

18
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:

Function execution context and this

A

The execution context for what we have learned so far, mostly affects what the this keyword refers to.

The execution context is defined at the time the function is executed.

If its just a global function, it will be implicitly set to reference the global object, in node. in strict mode it’s undefined

If a method, a function referenced by an object, is called, this is set to the object that method is on:

function purple() {
console.log(this);
}

let obj1 = {
purple: purple,
}

purple();

obj1.purple();

It can be explicitly set with call or apply and permanently bound to an object with .bind

19
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:

Implicit and explicit execution context

A

Unless otherwise stated, the execution context will be set implicitly. Either the global object, or the object a method was called on.
function purple() {
console.log(this);
}

let obj1 = {
purple: purple,
}

purple();

obj1.purple();

But you can set the execution context, as call time with .call or .apply.
OR you can create a new function with the execution context and certain arguments bound to the arguments.

20
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:
Dealing with context loss

A

Context isn’t really lost, in the sense that the basic rules haven’t been broken. So it’s not any thing within this method, has this object as its execution context. If a function is called within this method, it doesn’t adopt the execution context of that method, it has its own impliicit execution context (unless if it’s an arrow function).
We call this context loss.
There are some strategies to deal with it, depends on how you want things to function.
Can work work .call, .apply, .bind, or in this case, probably just an arrow function would be appropriate.

21
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:
call, apply, and bind

A

.call calls a function within a certain execution context, with this set to the first argument.
the following parameters will act as the arguments for the function

.apply does the exact same thing. Except instead the arguments for the function are given in an array.

22
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:

Object.assign and Object.create

A

Object.assign take the properties from the second object argument and assigns them to the first.
The first is mutated.
It also returns a reference to the first object argument.

Object.create creates a new blank object, and sets its prototype to the object argument or null.

23
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:

Built-in constructors like Array, Object, String and Number

A

These are functions on the global object

They have static methods
Prototype methods
They can act as constructors
They can be called as regular function

Finally when you create arrays, objects, and functions, these are automatically set as prototypes for those objects.
WIth primitives, something happens where the String and Number object are said to become temporary wrappers when you call a prototype method.

24
Q

Can you explain, to someone who doesn’t know about it and give any useful examples:

instanceof

A

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

instanceof checks to see whether the object property on a function after intanceof is a prototype of the object before instanceof (and named prototype)

So either created using new Contructor()

or

let obj1 = function() {};
obj1.prototype = {};
let obj2 = {};
Object.setPrototypeOf(obj2, obj1.prototype);
console.log(obj2 instanceof obj1);

25
Q

How do you create a Constructor so it can run without the new keyword.

A

function Dog(name) {
if (!(this instanceof Dog)) {
return new Dog(name);
}
this.name = name;
}

Dog.prototype.bark = function() {
console.log(${this.name} says: Woof! Woof!);
};

let fido = Dog(‘Fido’);
fido.bark(); // ‘Fido says: Woof! Woof!!’
console.log(fido instanceof Dog); // true