JS-201-ReactFundamentals
  • Part 0: App Overview
  • Part 1: Intro to React
    • 1.0: Packages Setup
    • 1.1: Project Setup
    • 1.2: React Router Dom
  • Part 2: JavaScript Concepts
    • 2.0: ES6 Overview
    • 2.1: classes
    • 2.2: constructors
    • 2.3: extends
    • 2.4: super
    • 2.5: interpolation
    • 2.6: map
    • 2.7: filter
  • Part 3: Functional Components
    • 3.0: Functional Components Overview
    • 3.1: Calling Functional Components
    • 3.2: Single Element Rule
    • 3.3: Arrow Functions
    • 3.4: Challenge
    • 3.4: Solution
    • 3.5: Challenge 2
    • 3.5: Solution 2
  • Part 4: JSX Challenge
    • 4.1: JSX Overview
    • 4.1: Challenge Answer
    • 4.2: className
  • Part 5: Class Concepts
    • 5.1: State
    • 5.2: setState
    • 5.3: Class Components Challenge
    • 5.4: Class Components Challenge Answer
  • Part 6: Props Starter
    • 6.0: Props Overview
    • 6.1: Props Demo and Challenge 1
    • 6.2: Props Answer 2
    • 6.3: Props Passing Functions and Challenge 2
    • 6.4: Props Answer 2
    • 6.5: External Props and Mapping Components
    • 6.6: PropTypes
  • Part 7: Lifecycle Methods
    • 7.0: Lifecycle Overview
    • 7.1: Lifecycle Methods
    • 7.2: Mounting Methods
    • 7.3: Update Methods
    • 7.4: Unmount Methods
  • Part 8: Apps
    • 1.0 - Small Timer Apps
      • 1.1 - Simple Timer
      • 1.2 - Clock App
      • 1.3 - Stop Watch App
      • 1.4 - Challenge
    • 2.0 - Concept List
      • 2.1 - Concept Data
      • 2.2 - Concept App Component
      • 2.3 - Concept Component
      • 2.4 - Concept List Component
    • 3.0 - NYT
      • 3.1 - NytApp Component
      • 3.2 - Nyt Results
    • 4.0 - The Friend List App
      • 4.1 - Friend List App Component
      • 4.2 - Friend Component
    • 5.0 - Movie Search Application
      • 5.1 - Form Component
      • 5.2 - Form Results Component
      • 5.3 - Styled Components
    • 6.0 - YouTube Api
      • 6.1 - Video Component
      • 6.2 - Video Component
      • 6.3 - Video Component
    • 7.0 - Github Api Application
      • 7.1 - The Users
      • 7.2 - Github API Component
      • 7.3 - Github API Card
      • 7.4 - Github API Card Form
      • 7.5 - Github API Card List
      • 7.6 - Github API Search
      • 7.7 - Github API Challenge
    • 8.0 - Bitcoin Api Application
      • 8.1 - Bitcoin API Setup
      • 8.2 - Bitcoin API Fetch
      • 8.3 - Bitcoin API Line Chart
      • 8.4 - Bitcoin API Fetching Data
      • 8.5 - Bitcoin API Info Box
      • 8.6 - Bitcoin API Completed Code
    • 9.0 - Google Maps Api Challenge
    • 10.0 - Sound Cloud App Challenge
    • 11.0 - VR App Challenge
    • 12.0 - React Native Intro
  • Part 9: Project
  • Part 10: Notes
    • 10.1 - Resources
    • 10.2 - Troubleshooting
Powered by GitBook
On this page
  • Creating a class
  • Making Objects/Instances
  • Notes
  1. Part 2: JavaScript Concepts

2.1: classes

Previous2.0: ES6 OverviewNext2.2: constructors

Last updated 7 years ago

If you don't already know, classes are a part of ES6. Classes are one of the most commonly used tools in React to build dynamic components, so it helps to have a strong foundation with classes before using them with React. Classes are not a new part of programming. Java, C#, and many other Object Oriented Languages have classes, which act as a blueprint for creating objects that share methods and properties.

But if you are coming from any kind of background with Java, C#, or C++, you should know that classes in JS aren't exactly the same as those languages. As the describe, they are syntactic sugar for JavaScript's prototypal inheritance. We recommend you study those docs, and familiarize yourself with the above classes even after we walk through it here.

Enough chatter. Let's get started.

Creating a class

Let's create a class called User:

//ES6 JS Classes
class User {
    constructor(name){
      this.name = name;
      this.type = "Trial User"
    }
    //Method 1
    greet(){
      console.log('Welcome back, ' + this.name);
    }
    //Method 2
    status(){
      console.log('Current status: ' + this.type);
    }
  }

Just a note for this module, let's ignore the constructor. We'll discuss that next.

Let's start with a higher level view of classes. We like to explain a class as a sort of blueprint for making objects. We'll also talk about it as a sort of cookie cutter. Think about this: If you had to make 1000 cookies for a 1000 kids, you are going to need a cutter. You're not going to use a kitchen knife, right? A class is the cutter that cuts cookies or objects, the actual tool, a blueprint for what kind of properties the cookie will have.

We use that class to make new objects or instances of the class. Each of these objects can have different values for properties. Icing would be a property of a cookie class. Each cookie will have it's own value for that property: white, red, blue, or no icing on that particular instance of cookie. Make sense?

Making Objects/Instances

We'll add to this class and break it down in future modules, but for now, let's learn to make objects/instances of the class. We make instances or objects from the class using the new keyword. Here's how we do that:

  //Instance of the class/new object
  var anonDude = new User("Anonymous dude");

  //we can now use the instance and the . operator 
  // to access the 2 methods
  anonDude.greet();
  anonDude.status();

  //Another instance of the class
  var anonLady = new User("Anonymous lady");
  anonLady.greet();
  anonLady.status();

   //Another instance of the class
  var jeff = new User("Jeff");
  jeff.greet();
  jeff.status();

When we run this in the Node console, we get the following result:

Notes

Here are a couple things to note: 1. We have three different instances of the User class. Three different cookies. 2. The . operator allows each instance of the class to 'access' the methods(functions) in the class. 3. Each instance has a different result in the console based on the string that is passed in to the constructor. Let's clarify that in the next module. 4. Before you move on, see if you can create 2 new instances of the class from memory.

MDN docs
Object Results