TIL 22 day 200922 session

Winney·2020년 9월 22일
0

200922 JS session

1. Class

예시 : 붕어빵틀

class 붕어빵틀 {
    constructor (반죽) {  //슈크림, 팥, 고추참치 등이 인자로 들어갈 수 있다. data
        this.반죽 = 반죽; 
    }

    bake = () => {};
    press = () => {};
}

class는 data와 함수를 가지고 있는 형태

const 슈크림붕어빵 = new 붕어빵틀(슈크림);

예시 : 인스타그램 Feed

class Feed {
    constructor(img, user, comments, likedCount) {
        this.img = img; // image url이 들어오기 때문에 string이 형태의 data가 넘어온다. 
        this.userId = userId; // id는 고유한 값이라 주로 number가 넘어온다.
        this.comments = comments; // array[string]이 넘어가는게 아니라 ***[{}] 형태로 넘어온다. 댓글에는 댓글 내용뿐만 아니라 누가 쓴지 유저정보도 들어가기 때문.
        this.likedCount = likedCount;
    }

    addComment = (comment ※객체가 인자로 들어감) => {
        this.comment.push(comment)
    };
    ※인자는 객체로 넘어간다.

    deleteComment = (commentId) => {
        this.comments = this.comments.filter((comment) => {
            if (comment.commentId === commentId) {
                return false; // 나는 ID를 지울것이기 때문
            } return true;
        });
    }
}


*** {
    userId: number,
    comment: string,
    commentId: number => comment에도 고유한 하나의 ID가 있다. 식별자.
}

형태로 배열에 담긴다. 여러 데이터를 받아와야 되기 때문에 객체를 사용한다.

배열은 복수로 작성해야한다.


2. object3 마지막 문제

for - in은 객체값을 하나씩 조회하고 싶을 때 쓴다. for-in문으로 객체를 돌릴 때 키값이 반환된다는 사실을 꼭 인지할 것
for - of는 배열의 값을 하나씩 조회하고 싶을 때 쓴다.

const getExamResult = (scores, requiredClasses) => {
  const result = {};
  const scoresToNumber = {
    "A+": 4.5,
    A: 4,
    "B+": 3.5,
    B: 3,
    "C+": 2.5,
    C: 2,
    "D+": 1.5,
    D: 1,
    F: 0,
  };
  
  for (const className in scores) {
    const score = scores[className];
    result[className] = scoresToNumber[score];
  }
  
  requiredClasses.forEach((className) => {
    if (!result[className]) result[className] = 0;
  });
  
  return result;
  
};

const scores = {
  생활속의회계: "C",
  논리적글쓰기: "B",
  독일문화의이해: "B+",
  기초수학: "D+",
  영어회화: "C+",
  인지발달심리학: "A+",
};

const requiredClasses = ["영어회화", "기초수학", "공학수학", "컴퓨터과학개론"];

const result = getExamResult(scores, requiredClasses);

console.log(result);
profile
프론트엔드 엔지니어

0개의 댓글