class, constructor, instance , OOP

Robin·2022년 4월 27일
0

개발상식

목록 보기
3/13

Class

  • Classes allow you to better organize your code by grouping your code (variables & functions) into a single class.
  • Classes promote reusability.
  • A class is a factory that is able to create instances.
  • Every instance created from a class is unique.
  • The common convention for the name of the class is UpperCamelCase(PascalCase).
  • We capture constructor params so that we can access them outside the constructor (in instance methods).
  • An instance variable is a variable that belongs to a specific instance of a class.
class User {
     // for instance variables
     constructor(firstName, lastName, age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

console.log(new User("Sam", "Blue", 18));
console.log(new User("Alex", "Green", 25));

Instance methods

  • Instance methods are functions that can be called on an instance of a class.
  • To be able to use instance variables inside an instance method, you have to prefix them with this. (as long as they were captured in the constructor)
  • Inside an instance method, this refers to the current instance of the class.
  • An instance method can call another instance method using the this.functionName() syntax.
class Course {
    constructor(name, isCompleted) {
        this.name = name;
        this.isCompleted = isCompleted;
    }

    getDescription() {
        if (this.isCompleted) {
            return `You have completed the ${this.name} course.`;
        } else {
            return `You are currently studying the ${this.name} course.`;
        }
    }
}

const course1 = new Course("Learn JavaScript", false);
console.log(course1.getDescription()); // "You are currently studying the Learn JavaScript course"
const course2 = new Course("Learn Programming", true);
console.log(course2.getDescription()); // "You have completed the Learn Programming course"

OOP

(ex) 객체 지향 프로그래밍

class User {
    constructor(firstName, lastName, prefix, age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.prefix = prefix;
        this.age = age;
    }

    getFullName() {
        return `${this.prefix}. ${this.firstName} ${this.lastName}`;
    }

    canVote() {
        return this.age >= 18;
    }
}

// Sample usage
const user1 = new User("Sam", "Doe", "Mrs", 20);
console.log(user1.getFullName()); // "Mrs. Sam Doe"
console.log(user1.canVote()); // true
const user2 = new User("Alex", "Green", "Mr", 17);
console.log(user2.getFullName()); // "Mr. Alex Green"
console.log(user2.canVote()); // false

(ex) 절차 지향 프로그래밍

const getFullName = (firstName, lastName, prefix) => {
    return `${prefix}. ${firstName} ${lastName}`;
}

const canVote = (age) => {
     return age >= 18;
}

// Sample usage
console.log(getFullName("Sam", "Doe", "Mrs")); // "Mrs. Sam Doe"
console.log(canVote(20)); // true
console.log(getFullName("Alex", "Green", "Mr")); // "Mr. Alex Green"
console.log(canVote(17)); // false
  • We don't have objects.
  • We just have 2 functions and we call them several times.
  • On the other hand, with the OOP example, we create an object user1 which is an instance of the class User.
profile
Always testing, sometimes dog walking

0개의 댓글