Javascript์์์ this๋ Java๋ณด๋ค ๋ ๋ค์ํ ๋์๋ฐฉ์์ผ๋ก ์ด๋ฃจ์ด์ง๋ค.
var info = {name: "euna"}
this.name์ euna๊ฐ ์ถ๋ ฅ๋์ด์ง๋ค.
๋ณ์ info๋ this๊ฐ ๋ฐ๋ผ๋ณด๋ ๊ฐ์ฒด๊ฐ ๋๋ค.
this์ ๋์ ๋ฐฉ์๋ค
์ฐ์ , this๋ฅผ ์ฝ์ ๋ก๊ทธ๋ก ์ฐ์ด๋ณด์
this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๋ฐ๋ผ๋ณด๋ ๊ฐ์ผ๋ก ๊ฐ๋๋ค. (์ฌ๊ธฐ์๋ window ๊ฐ์ฒด)
console.log(this);
- ์ผ๋ฐ ํจ์์์์ This : ์ผ๋ฐํจ์์์๋ ์ ์ญ ๊ฐ์ฒด์ธ window๊ฐ ์ ์ฅ
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>JavaScript์์์ this</title>
</head>
<body>
<script>
var data = 10;
console.log(this.data); //10
function thisFunSample() {
console.log(this) //window ๊ฐ์ฒด
this.data = 20;
console.log("1. data : " + data); //20
console.log("2. this.data : " + this.data); //20
console.log("3. window.data : " + window.data); //20
data = 30;
console.log("1. data : " + data); //30
console.log("2. this.data : " + this.data); //30
console.log("3. window.data : " + window.data); //30
}
thisFunSample();
</script>
</body>
</html>
- ์ผ๋ฐ ์ค์ฒฉ ํจ์์์์ This : ์์ ๋์ผ
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>JavaScript์์์ this</title>
</head>
<body>
<script>
var data = 10;
function thisFunSampleOut() {
function thisFunSampleIn() {
this.data = 20;
console.log("1. data : " + data); //20
console.log("2. this.data : " + this.data); //20
console.log("3. window.data : " + window.data); //20
data = 30;
console.log("1. data : " + data); //30
console.log("2. this.data : " + this.data); //30
console.log("3. window.data : " + window.data); //30
}
thisFunSampleIn();
}
console.log(this.data); //10
thisFunSampleOut();
</script>
</body>
</html>
- Events Listener์์์ This : this๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ ๊ฐ์ฒด
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>JavaScript์์์ this</title>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous">
</script>
</head>
<body>
<script>
var data = 10;
$(document).ready(function() {
//# : id
//.~ : class
$("#thisButton").click(function(){
this.data = 20;
console.log("1.data : " + data); //10
console.log("2.this.data : " + this.data); //20
console.log("3.window.data : " + window.data); //10
data = 30;
console.log("1.data : " + data); //30
console.log("2.this.data : " + this.data); //20
console.log("3.window.data : " + window.data); //30
});
});
</script>
<input type="button" id="thisButton" value="๋ฒํผ">
</body>
</html>
- ๋ฉ์๋์์์ This : ๊ฐ์ฒด ์์ ์ด ์ ์ฅ
๋ฉ์๋ : ๊ฐ์ฒด๋ฅผ ํตํด ์ํ (java๋ก ๋ฐ์ง๋ฉด class์ ์ข ์๋ ๊ฒ๋ค์ ์๋ฏธํจ. ํด๋์ค ํจ์)
ํจ์ : ๋ ํฌ๊ด์ ์๋ฏธ. ๋ ๋ฆฝ์ ์ผ๋ก ์กด์ฌ.
๊ฐ์ฒด์ ๋ฉ์๋๋ก์ function์ ํธ์ถํด๋ณด๋ฉด, ๋ฉ์๋์์์ this์ ์๋ฏธ๋ฅผ ์ ์ ์๋ค.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>JavaScript์์์ this (๋ฉ์๋์ this)</title>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous">
</script>
</head>
<body>
<script>
var data = 10;
console.log(this.data); //10
function func()
{
this.data = 100;
console.log(this.data); //100
console.log(window.data); //100
}
func();
var callFun =
{
data : "ํ
์คํธ",
say : function() { //์ผ๋ฐ ํจ์๊ฐ ์๋ ๋ฉ์๋ ๊ตฌํ
return this.data;
}
};
//๋ฉ์๋ call
console.log(callFun.say()); //ํ
์คํธ
</script>
</body>
</html>
'๐ Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[javascript] ๋ฐฐ์ด ์ ๊ฐ์ฒด๋ค์ ๊ฐ ์์ ๋ํ๊ธฐ (0) | 2023.02.02 |
---|---|
[javascript] <input> ํ๊ทธ์ ์ซ์๋ง ์ ๋ ฅ (์ ๊ท์ ์ฌ์ฉ) (0) | 2023.01.27 |
[Javascript] ES5, 6 ์ฐจ์ด (0) | 2022.05.23 |
[Javascript] ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ์ด (0) | 2022.04.29 |
[Javascript] instanceof, arguments (0) | 2022.04.29 |