Skip to content

Instantly share code, notes, and snippets.

@jonnung
Last active August 29, 2015 14:17
Show Gist options
  • Save jonnung/0f5cf21e08af0ddb9088 to your computer and use it in GitHub Desktop.
Save jonnung/0f5cf21e08af0ddb9088 to your computer and use it in GitHub Desktop.
자바스크립트의 this를 이해하기 위한 문제
// ############ EXAMPLE ############
// CASE1
var obj = {
prop: 'EWJO',
sub_func: function () {
var user = obj.sub_func2();
console.log('Hello ' + user);
},
sub_func2: function () {
return this.prop;
}
};
obj.sub_func();
// 기대하는 결과: Hello EWJO
// ############ QUESTION ############
// CASE2
msg = 'CASE2 TEST!!';
var func = function () {
var nickname = 'jonnung';
var upper_case = function (txt) {
var msg = 'Success to upper case!';
console.log(this.msg);
return txt.toUpperCase();
};
return {
prop: 'EWJO',
sub_func: function () {
var user = func.sub_func2();
console.log('Hello ' + user + '. My nickname is ' + func.upper_case(func.nickname));
},
sub_func2: function () {
return this.prop;
}
};
};
var obj2 = func();
obj2.sub_func();
// 문제1: CASE1과 CASE2의 차이점은 무엇인가요?
// 문제2: 아래와 같이 console 에 출력되도록 CASE2 코드를 수정하고, 그 이유를 설명하세요.
// Success to upper case!
// Hello I'm EWJO. My nickname is JONNUNG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment