-
카테고리
-
세부 분야
풀스택
-
해결 여부
미해결
this 질문
19.05.29 14:07 작성 조회수 94
0
var relationship1 = {
name: "zero",
friends: ["nero", "hero", "xero"],
logFriends: function() {
var that = this;
this.friends.forEach(function(friend) {
console.log(that.name, friend);
});
}
};
relationship1.logFriends();
여기서 this를 that에 대입을 하는 이유가 뭔가요?
logFriends 안에서 콘솔로그로 this를 보니까 Object[global] ~~ 이런게 뜨던데 그럼 this를 that에 대입해도 똑같은 값이 대입되어야 하지 않나요?
그런데 that에 대입을 하고 콘솔로그해보면 relationship1의 객체가 나오네요..
이유가 궁금합니다.
답변을 작성해보세요.
1
조현영
지식공유자2019.05.29
function 안에서 this는 바깥 함수의 this와 다릅니다. 따라서 바깥 함수의 this를 사용하려면 다른 변수에 값을 대입해서 사용해야 합니다.
답변 1