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.