Exam checklist Flashcards
Can you explain, to someone who doesn’t know about it and give any useful examples:
Object Oriented Programming
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
object factories,
Advantages and disadvantages vs constructors.
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
constructors
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
Can you explain, to someone who doesn’t know about it and give any useful examples:
prototypes
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
OLOO
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
ES6 classes
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
Prototypal and pseudo-classical inheritance
Prototypal inheritance is inheritance object to object.
Pseudo classical inheritance is when the prototype is an object on a constructor
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
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
Encapsulation
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
Polymorphism
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
Collaborator objects
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
Single vs multiple inheritance
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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
Mix-ins; mix-ins vs. inheritance
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.
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 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.
Can you explain, to someone who doesn’t know about it and give any useful examples:
Higher-order functions
A higher order function takes one or more function objects as arguments and/or returns a function object as its result.