JavaScript OOP Flashcards

1
Q

What is OOP?

A

“Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions”.”

OOP is a set of principles that we should follow if we want to use objects as the primary way of organizing our code.
Some of these principles are supported by language features

Others are more abstract principles we must follow when we write code

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

What are classes and why are they one of the fundamental OOP features in JS?

A

Classes are the fundamental language feature that allows us to use OOP

Classes are object templates that are used to model:

  • State (using variables that we call fields)
  • Behavior (using functions that we call methods)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the class state?

A

Fields are class data members

  • They are the equivalent of variables
  • They define the object state

Fields can have a default value

• This value will be applied to every object created from this class

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

What is class behavior?

A

Methods are class members that execute some action

  • Just like a regular method that is part of an object
  • Related fields and methods should be put in the same class
  • Methods can interact with the fields
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are class instances and how can you create one?

A

Let’s take a look at this simplified Person class and how we can create objects (also known as an instance) from it

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

How can create instances with constructors?

A

Constructors are kind of special methods

  • Invoked at the time of creating a new instance of an object
  • Used to initialize the fields of the instance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are static members and what are they used for?

A

The static keyword defines a static field or method for a class.

  • Static members aren’t called on instances of the class.
  • Instead, they’re called on the class itself.
  • We use static fields when we want all instances to be able to access and/or modify.
  • Static members are initialized just before the type is used for the first time
  • There lifetime is the entire application lifetime
  • Static members do not have access to this keyword

Static members are used when the type should have or do a specific thing that is not bound to any instance

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

Instance vs Static

A

Instance:

• Associated with an instance (object)•Initialized when the constructor is called•

Static:

• Associated with a type (class), not with an instance•Initialized just before the type is used for the first time•

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

What is the difference between OOP and FP?

A

Functional programming - Organizing your code into multiple functions where each function works on its own

The advantage of the functional approach is that you have:

  • A bunch of highly re-usable (and manageable) functions with little to no dependencies
  • A very easy to maintain and understand code
  • Object-oriented programming - Organizing your code in classes and objects (objects are based on classes)

Your data is stored in properties, and your logic in methods

• Data and logic that work closely together live in the same class

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