vue3 ref 구현하기

yoeubi
Feb 11, 2021

--

ref를 구현하기 전에 먼저 getter/setter를 알아야 한다.

Getter/Setter

getter/setter는 접근자 프로퍼티(accessor property)입니다.

getter는 프로퍼티를 읽을때 실행되고 setter는 프로퍼티에 값을 할당할때 실행됩니다.

getter/setter를 사용해서 프로퍼티를 원하는대로 컨트롤 할 수 있습니다.

이처럼 값을 특정한 조건이나 변환을 가할수 있습니다.

Ref

vue3에서 사용하는 ref도 이러한 getter/setter를 사용해서 구현되어 있습니다.

실제 ref 코드는 훨씬 더 길고 복잡한 조건문들이 걸려있지만 코어부분만 간추렸습니다.

vue3에서는 단순하게 객체(object)가 아닌 클래스(class)로 getter, setter를 사용했습니다.

객체와 클래스의 getter/setter는 사용법에 있어서 큰 차이점은 없습니다.

객체는 객체안에 getter/setter가 설정되지만 class는 prototype에 설정됩니다.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

yoeubi
yoeubi

Written by yoeubi

Junior Frontend engineer

No responses yet

Write a response