1. 01. 추상화
# 추상화
: 어떤 구체적인 존재를 원하는 방향으로 간략화해서 나타내는 것
- 클래스를 설계하는 것도 추상화
- 클래스 이름, 프로퍼티 이름, 메소드 이름을 직관적으로, 이해하기 쉽도록 잘 정하는 것이 중요
2. 03. 캡슐화
# 캡슐화
: 객체의 특정 프로퍼티에 직접 접근하지 못하도록 막는 것
- 안전성 높일 수 있음
- 멤버변수에 대한 직접 접근을 제한하려는 목적으로(혹은 getter, setter 이름과 구분을 위한 목적으로) 멤버변수의 이름을 언더바(_)로 시작하게 지정
# setter method
<javascript />
set email(address){
if(address.includes('@')){
this._email = address;
}else{
throw new Error('invaild email address');
}
}
# getter method
<javascript />
get email(){
return this._email;
}
3. 05. 캡슐화 직접 해보기
- main.js
<javascript />
class BankAccount {
constructor(name, money) {
this.holder = name;
this.balance = money;
}
get balance() {
return this._balance;
}
set balance(money){
if(money>=0){
this._balance = money;
}else{
console.log('You cannot set negative number for balance');
}
}
deposit(money) {
this.balance += money;
}
withdraw(money) {
if (this.balance - money < 0) {
console.log('Insufficient balance');
} else {
this.balance -= money;
}
}
transfer(money, anotherAccount) {
const account = anotherAccount;
if (this.balance - money < 0) {
console.log('Insufficient balance');
} else {
this.balance -= money;
account.balance += money;
}
}
}
const account1 = new BankAccount('Michael', 10000);
account1.balance = 20000;
account1.balance = -5000;
4. 06. 상속
# 상속
: 하나의 객체가 다른 객체의 프로퍼티와 메소드를 물려받는 것
- 상속을 하면 겹치는 부분은 다시 쓸 필요x
- 코드의 재사용성이 좋아짐
5. 07. super
- 자식 클래스의 생성자 함수에서 부모 클래스의 생성자 함수를 super 키워드로 호출해야함
<javascript />
class User{
constructor(email,birthdate){
this.email = email;
this.birthdate = birthdate;
}
buy(item){
console.log(`${this.email} buys ${item.name}`);
}
}
class PrimiumUser extends User{
constructor(email, birthdate, level){
super(email,birthdate);
this.level = level;
}
streamMusicForFree(){
console.log(`Free music streaming for ${this.email}`);
}
}
6. 08. 상속 직접 해보기
- main.js
<javascript />
class BankAccount {
constructor(name, money) {
this.holder = name;
this.balance = money;
}
get balance() {
return this._balance;
}
set balance(money) {
if (money >= 0) {
this._balance = money;
} else {
console.log('Not valid');
}
}
deposit(money) {
this.balance += money;
}
withdraw(money) {
if (this.balance - money < 0) {
console.log('Insufficient balance');
} else {
this.balance -= money;
}
}
transfer(money, anotherAccount) {
const account = anotherAccount;
if (this.balance - money < 0) {
console.log('Insufficient balance');
} else {
this.balance -= money;
account.balance += money;
}
}
}
class SavingsAccount extends BankAccount {
constructor(name, money){
super(name, money);
this.years = 0;
}
addInterest(rate){
this.balance *= (1+(rate*this.years));
}
}
class DonationAccount extends BankAccount{
constructor (name, money, rate){
super(name, money);
this.rate = rate;
}
donate(){
this.balance *= (1-this.rate)
}
}
const sa1 = new SavingsAccount('Kate', 50000);
const da1 = new DonationAccount('Mike', 90000, 0.05);
sa1.years++;
sa1.addInterest(0.02);
da1.donate();
sa1.years++;
sa1.addInterest(0.05);
da1.donate();
sa1.years++;
sa1.addInterest(0.07);
da1.donate();
console.log(Math.floor(sa1.balance));
console.log(Math.floor(da1.balance));
7. 09. 다형성
# 다형성
: 많은 형태를 갖고 있는 성질
# 오버라이딩
: 자식 클래스에서 부모 클래스의 메소드를 이름을 똑같이 하여 새롭게 정의하는 것 (덮어쓰는 것)
<javascript />
class User{
constructor(email,birthdate){
this.email = email;
this.birthdate = birthdate;
}
buy(item){
console.log(`${this.email} buys ${item.name}`);
}
}
class PrimiumUser extends User{
constructor(email, birthdate, level){
super(email,birthdate);
this.level = level;
}
buy(item){
console.log(`${this.email} buys ${item.name} with a 5% discount`);
}
streamMusicForFree(){
console.log(`Free music streaming for ${this.email}`);
}
}
8. 10. 부모 클래스의 메소드가 필요하다면?
- super 키워드로 부모 클래스의 메소드를 그대로 물려받아 사용
<javascript />
class User{
constructor(email,birthdate){
this.email = email;
this.birthdate = birthdate;
}
buy(item){
console.log(`${this.email} buys ${item.name}`);
}
}
class PrimiumUser extends User{
constructor(email, birthdate, level, point){
super(email,birthdate);
this.level = level;
this.point = point;
}
buy(item){
super.buy(item);
this.point += item.price*0.05;
}
streamMusicForFree(){
console.log(`Free music streaming for ${this.email}`);
}
}
9. 11. 다형성 적용해보기
<javascript />
class BankAccount {
constructor(name, money) {
this.holder = name;
this.balance = money;
}
get balance() {
return this._balance;
}
set balance(money) {
if (money >= 0) {
this._balance = money;
} else {
console.log('Not valid');
}
}
deposit(money) {
this.balance += money;
}
withdraw(money) {
if (this.balance - money < 0) {
console.log('Insufficient balance');
} else {
this.balance -= money;
}
}
transfer(money, anotherAccount) {
const account = anotherAccount;
if (this.balance - money < 0) {
console.log('Insufficient balance');
} else {
this.balance -= money;
account.balance += money;
}
}
}
class SavingsAccount extends BankAccount {
constructor(name, money) {
super(name, money);
this.years = 0;
}
addInterest(rate) {
this.balance *= (1 + (rate * this.years));
}
transfer(money, anotherAccount) {
super.transfer(money, anotherAccount);
this.balance -= money * 0.005;
}
}
class DonationAccount extends BankAccount {
constructor(name, money, rate) {
super(name, money);
this.rate = rate;
}
donate(rate) {
this.balance *= (1 - this.rate);
}
transfer(money, anotherAccount) {
super.transfer(money, anotherAccount);
this.balance -= money * 0.002;
}
}
const ba1 = new BankAccount('Tom', 20000000);
const sa1 = new SavingsAccount('Jerry', 10000000);
const da1 = new DonationAccount('Kate', 30000000);
const sa2 = new SavingsAccount('Alice', 9000000);
const accountForVacation = new BankAccount('Vacation', 0);
const accounts = [ba1,sa1,da1,sa1];
accounts.forEach((account)=>account.transfer(800000,accountForVacation));
console.log(ba1.balance);
console.log(sa1.balance);
console.log(da1.balance);
console.log(sa2.balance);
console.log(accountForVacation.balance);
10. 12. instanceof 연산자
# instanceof 연산자
: 객체가 특정 클래스에 속하는지 아닌지를 확인
- true / false 로 리턴
- 자식 클래스는 부모클래스에 속함
11. 13. static 프로퍼티와 static 메소드
- 객체가 아닌 클래스 자체로 접근
# static 프로퍼티
: 클래스에 직접적으로 딸려있는 프로퍼티
# static 메소드
: 클래스에 직접적으로 딸려있는 메소드
<javascript />
class Math{
static PI = 3.14;
static getCircleArea(radius){
return Math.PI * radius * radius;
}
}
console.log(Math.PI);
console.log(Math.getCircleArea(5));
'FRONTEND > JavaScript' 카테고리의 다른 글
[ 노마드코더 - 바닐라 JS로 크롬 앱 만들기 ] 소스코드 및 완성본 (0) | 2022.12.22 |
---|---|
[ 생활코딩 - WEB2 JavaScript ] 강의 내용 정리 (0) | 2022.12.08 |
[코드잇 - 자바스크립트 객체 지향 기본기] 1. 객체와 클래스 (0) | 2022.11.22 |
[코드잇 - 모던 자바스크립트] 06. 자바스크립트 모듈 (0) | 2022.11.22 |
[코드잇 - 모던 자바스크립트] 05. 자바스크립트의 유용한 내부 기능 (0) | 2022.11.21 |