-
카테고리
-
세부 분야
프론트엔드
-
해결 여부
해결됨
observable의 함수를 익명함수로 바꿔봤습니다.
23.06.23 19:04 작성 23.06.23 19:10 수정 조회수 225
0
const userStore = observable({
isLogginIn: false,
data: null as any,
logIn(data: any) {
this.isLogginIn = true;
setTimeout(() => {
this.data = data;
this.isLogginIn = false;
postStore.data.push(1);
}, 2000);
},
logOut() {
this.data = null;
},
});
const userStore = observable({
isLogginIn: false,
data: null as any,
logIn: (data: any) => {
userStore.isLogginIn = true;
setTimeout(() => {
userStore.data = data;
userStore.isLogginIn = false;
postStore.data.push(1);
}, 2000);
},
logOut: () => {
userStore.data = null;
},
});
평소에 선언적함수보다 익명함수로 코딩을 하고 있어서 코딩스타일을 맞추고 싶어
위 코드를 밑에처럼 익명함수로 바꿔봤습니다.
익명함수에 화살표함수를 쓰니 this가 바인딩되어
undefind가 된것 같은데 그래서 this 대신 userStore에서 접근하는 방식으로 바꿔봤습니다.
this대신에 useStore로 접근해도 괜찮은지 잘 모르겟습니다.
답변을 작성해보세요.
0
grs0412
질문자2023.06.23
개인적인 궁금증입니다만
this라는게 불러오는 주체에 따라 바뀌어서 bind를 하고 항상 this가 뭔지 생각을 하게되는데
그럼에도 this를 썻을때 이점이 있을까요? 선언된 상수가 아니라 this가 더 많이 쓰이는 이유가 궁금합니다.
조현영
지식공유자2023.06.24
클래스의 경우를 생각해보시면 될 것 같습니다. 클래스의 경우에는 this를 쓰지 않으면 가리킬 수가 없습니다.
객체의 경우가 조금 특별한 상황이고요
답변 1