inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

따라하며 배우는 노드, 리액트 시리즈 - 레딧 사이트 만들기(NextJS)(Pages Router)

Post Entity

protected userVote: number;

236

반가우면반갑다고해

작성한 질문수 142

0

강사님 안녕하세요,

 protected userVote: number;

  setUserVote(user: User){
    const index= this.votes?.findIndex(v=> v.username === user.username);
    this.userVote = index > -1 ? this.votes[index].value : 0;
  }

위의 코드에서 protected userVote: number;
이 부분을 어떻게 받아들여야할지 모르겠습니다

예전에 자바를 겉핥기로 본적이있어서 의미하는 바는 대강알겠는데
protected userVote: number; 이거를 명시해준거는

let userVote; 이렇게 혹은 const userVote; 이런식으로 명시해준것과 같은걸까요?

postgresql typescript nodejs docker react 클론코딩 Next.js

답변 1

1

John Ahn

안녕하세요!

넵 그렇게 생각해주시면 됩니다.
하지만 다른 점은
클래스의 접근 제한자를 이해해주셔야 하는데
여기 쓰여있는 protected 같은 경우는

class Person {
    protected name: string;
    constructor(name: string) { this.name = name; }
}
class Employee extends Person {
    private department: string;
    constructor(name: string, department: string) {
        super(name);
        this.department = department;
    }
    public getElevatorPitch() {
        return `Hello, my name is ${this.name} and I work in ${this.department}.`;
    }
}
let howard = new Employee("John Smith", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // error

 

Person 클래스의 외부에서 protected name 이라고 해준 name을 사용할 수는 없습니다.
사용하려고 하면
let han = new Person("Han"); // Error: The 'Person' constructor is protected
이런 식의 에러를 내보냅니다.
그 대신 Employee 클래스가 Person 부모 클래스를 확장해서 사용하고 있기에
protected 된 name 을 Employee 클래스 안에 있는 메소드에서는 사용할 수 있습니다.
그래서
let howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());

이 부분은 가능하게 되는 것입니다!
이 부분에 관해서는
typescript 접근 제한자를 구글링 하시면 더 많은 정보를 얻을 수 있을 거 같습니다!
https://infoscis.github.io/2017/05/20/TypeScript-handbook-classes/
여기에 되게 잘 나와있네요 !!

감사합니다!


 

toJson을 추가하면 [sub].tsx에서 sub를 받아오지 못합니다.

0

122

2

쿠키 저장이 되지 않습니다.

0

226

1

AxiosError {message: 'Request failed with status code 401/500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', (2)

0

581

1

AxiosError {message: 'Request failed with status code 401/500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE',

0

651

1

overload 에러

0

170

1

docker compose up 오류

0

210

1

부록) remark 강의 중 parmas 오류

0

140

1

3000번은 잘 들어가지는데 80번은 안됩니다.

1

296

0

커뮤니티를 올리고 난 후 404 page

0

209

1

tailwind css 문제인지, className 에 적용한 css가 적용되지 않아요.

0

1043

2

tsx 수정 시 마다 빌드 후 서버 시작 해야하나요?

0

656

2

useState 쳤을 때 자동완성 되는 단축키 무엇인가요? extention 인가요?

0

805

2

리액트 서버 npm run dev 와 npm run build 후 npm start 의 차이

0

4615

2

data 폴더가 생성되지 않아요.

0

527

1

docker-compose up 오류

0

814

1

회원 가입 페이지 기능 생성(3) 중 에러

0

380

2

Entity에 toJSON 코드 입력 후 404 에러

0

252

1

context에서 useEffect 선언 부분 질문 있어요.

0

297

1

src 폴더구조

0

511

2

서버 실행 시 에러 관련하여 답변받고 1차 조치했는데 여전하여서 질문 남깁니다

0

298

1

엔티티 모두 작성 후 서버 실행 시 에러가 발생합니다

0

313

1

회원가입 누르면 404에러가 뜹니다 ;-;

1

424

1

nextjs버젼에 대해서 질문드립니다.

0

373

1

<npm run dev>시 -61 에러가 나타납니다!

0

359

1