C++¿¡¼ this´Â ¿ÀºêÁ§Æ® ÀÚ½ÅÀ» ³ªÅ¸³»Áö¸¸ ÀÚ¹Ù½ºÅ©¸³¿¡¼ this´Â ±¸¹®ÀÇ »óÅ¿¡ µû¶ó
´Þ¶óÁø´Ù. »óȲ¿¡ this°¡ ±Û·Î¹ú(Window)ÀÇ thisÀÎÁö ¿ÀºêÁ§Æ®ÀÇ thisÀÎÁö ¾Ë¾Æ º¸ÀÚ. 1. ÇÔ¼ö ½ÇÇà ÇÔ¼ö¿¡¼ this´Â ±Û·Î¹úÀÌ´Ù. function doFunc() {
console.log('doFunc this instanceof Window: ' + (this instanceof Window)); } doFunc(); ½ÇÇà °á°ú) doFunc this instanceof Window: true 2. ¿ÀºêÁ§Æ® ¸Þ¼Òµå ½ÇÇà ¿ÀºêÁ§Æ® ¸Þ¼ÒµåÀÇ this´Â ¿ÀºêÁ§Æ®ÀÇ thisÀÌ´Ù var age = 20;
var student = { age: 5, getAge: function () { console.log('this.age: '+ this.age + ' age: ' + age); } } student.getAge(); ½ÇÇà °á°ú) this.age: 5 age: 20 3. »ý¼ºÀÚ ½ÇÇà »ý¼ºÀÚ¿Í ¿ÀºêÁ§Æ® ¸Þ¼ÒµåÀÇ this´Â ¿ÀºêÁ§Æ®ÀÇ thisÀÌ´Ù function Human() {
console.log('this instanceof Human: ' + (this instanceof Human)); this.getInfo = function() { console.log('this instanceof Human.getInfo: ' + (this instanceof Human)); } } var human = new Human(); human.getInfo(); ½ÇÇà °á°ú) this instanceof Human: true this instanceof Human.getInfo: true var getInfo = function() {
console.log('this instanceof Human.getInfo: ' + (this instanceof Human)); } function Human() { console.log('this instanceof Human: ' + (this instanceof Human)); this.getInfo = getInfo; } var human = new Human(); human.getInfo(); ½ÇÇà °á°ú) this instanceof Human: true this instanceof Human.getInfo: true 4. Áï½Ã ½ÇÇà ÇÔ¼ö ½ÇÇà Áï½Ã ½ÇÇà ÇÔ¼öÀÇ this´Â ±Û·Î¹úÀÌ´Ù. Áï½Ã ½ÇÇà ÇÔ¼ö ³»ºÎÀÇ age´Â ·ÎÄà º¯¼ö´Ù. var age = 20;
var Man = (function () { var age = 7; var output = function() { console.log('Man this instanceof Window: ' + (this instanceof Window)); console.log('this.age: '+ this.age + ' age: ' + age); } function getAge() { return age; } function setAge(manAge) { age = manAge; output(); } return { getAge: getAge, setAge: setAge }; })(); Man.setAge(21); ½ÇÇà °á°ú) Man this instanceof Window: true this.age: 20 age: 21 Å×½ºÆ® ¼Ò½º : this_test.html Âü°í) this¿¡ ´ëÇÑ ¼³¸í http://webframeworks.kr/tutorials/translate/explanation-of-this-in-javascript-1/ Áï½Ã ½ÇÇà ÇÔ¼ö http://beomy.tistory.com/9 JavaScript Function Call https://www.w3schools.com/js/js_function_call.asp |