JS OOP Flashcards
What is a method?
a function which is a property of an object. In JS functions themselves are objects. So a method is actually an object reference to a function.
What does a method do?
they are actions that can be performed on objects
What is this?
this keyword refers to the object it belongs to. It refers to the global object in a function
What does bind do?
bind() method creates a new function when invoked, has the this sets to the provided value. It allows an object to borrow a method from another object without making a copy of that method
What is the difference between a function and an object literal?
a function you create an object that can be instantiated into multiple instances, while the literal notation delivers a single object.
What is Prototypal Inheritance?
inheritance from other prototypes of Objects
What is the Prototypal Chain?
all objects in JS are instances of objects which sits on the top of the prototypal chain
In the custom object we created, where did the other __proto__ come from?
from the curly braces
Why does JS have prototypal inheritance?
when you create it once, you save memory when you only need to do it once. Basically for the sake of memory
What does the new keyword do?
creates an empty object, then sets the constructor object to another object, then it gets passed into the function as this, lastly returns this if the function doesn’t return an object.
I could’ve added a method to the constructor function by assigning a method to a property on the this object. Why do we have to add it to the prototype property?
because of inheritance and memory efficiency
What is the first thing that happens in a class when it is instantiated with the new keyword?
calls the constructor function
Since classes are technically functions as well. Can you name a difference between classes and functions?
classes don’t hoist. functions can hoist
What is a static method?
they are not called on instances of the class but instead they’re called on the class itself
What is the benefit of instantiating Classes within other classes?
it enables you to group classes that are only used in one place.
Why are parent - child relationships important in OOP?
you can reuse functionality common to all the object types rather than having to duplicate it. By having connection with class within another class.
What is the basic idea of OOP?
we use objects to model real world things that we want to represent inside our programs or provide a simple way to access functionality that would otherwise be hard. To avoid spaghetti code.
Why is it important to assign callback methods to properties in the constructor?
in order to save it. to reference it in the future
Why did the maker class require all the parent class info?
the maker is responsible for making the parent class
Why did you have to bind this for the replenishfood method?
so that it would work when passed forward to the parent class
What decides the order in which JS files must be loaded?
if its calling a data, you have to include it first. If you want to use a tool, it needs to exist already.