์๋ฐ์คํฌ๋ฆฝํธ์์ this๋ ์คํ ์ปจํ
์คํธ๊ฐ ์์ฑ๋ ๋ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
์ฆ, ํจ์๊ฐ ํธ์ถ๋ ๋ this๊ฐ ๊ฒฐ์ ๋๋ค๋ ๊ฒ์
๋๋ค.
์๋ ์์ค๋ฅผ ๋ณด๋ฉด ์ด๋ป๊ฒ ํธ์ถํ๋๋์ ๋ฐ๋ผ this๊ฐ ๋ฌ๋ผ์ง๋๋ค.
var student = {
name: "jennie",
myName: function () {
console.log(this.name);
},
};
var myName = student.myName;
myName(); // undefined
student.myName(); // jennie
๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅธ ์ด์ ๋ myName()
์ ํจ์๋ก ํธ์ถ์ ํ๊ณ student.myName()
๋ ๋ฉ์๋๋ก ํธ์ถํ๊ธฐ ๋๋ฌธ์
๋๋ค.
ํจ์ ํธ์ถ ์
๋ชจ๋ ๊ฐ์ฒด๋ ์ ์ญ ๊ฐ์ฒด์ ํ๋กํผํฐ์ ๋๋ค
- ์ ์ญ ์ปจํ ์คํธ๋ฅผ ์คํํ๋ ์ฃผ์ฒด๋ ์ ์ญ ๊ฐ์ฒด์ ๋๋ค.
- ๋ชจ๋ ์ ์ญ๋ณ์์ ํจ์๋ window ๊ฐ์ฒด์ ํ๋กํผํฐ์ ๋๋ค.
- a์
window.a
๋ ๊ฐ๊ณ , foo();์window.foo();
์ ๊ฐ์ต๋๋ค. - ๊ฐ์ฒด๋ฅผ ๋ช ์ํ์ง ์์ผ๋ฉด ์์์ ์ผ๋ก window์ ํ๋กํผํฐ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
var a = "Hello?";
function foo() {
console.log("world");
}
console.log(a); // Hello?
console.log(window.a); // Hello?
foo(); // world
window.foo(); // world
์ผ๋ฐ ํจ์๋ก ํธ์ถ ์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค
์๋ ์์ค์ ๊ฐ์ด ์ผ๋ฐ ํจ์๋ก ํธ์ถ ์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.(๋ธ๋ผ์ฐ์ ์์๋ window, ๋ ธ๋์์๋ global)
function foo() {
function a() {
console.log(this); // window
}
a(); // ์ผ๋ฐ ํจ์ ํธ์ถ
}
foo();
var obj = {
foo: function () {
function f() {
console.log(this); //window
}
f(); // ์ผ๋ฐ ํจ์ ํธ์ถ
},
};
obj.foo();
use strict ๋ชจ๋์ผ ๋
์ค๋ฅ๋ฅผ ๋ณด์ํด ์ฃผ๊ธฐ ์ํด ์๊ฒฉํ ์ฝ๋ ์คํ์ ํด์ฃผ๋ ํ๊ฒฝ
"use strict";
var a = "hello";
function foo() {
console.log(this.a); // this === undefined
}
foo();
- ์ ์์ค์ ๊ฐ์ด
use strict
๋ชจ๋๊ฐ ์ ์ฉ๋๋ฉดthis === undefined
๋ก ์ค์ ๋ฉ๋๋ค. - ๋ง์ฝ window๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์ถ๋ค๋ฉด ์๋์ ๊ฐ์ด window๋ฅผ ๋ช ์ํฉ๋๋ค.
"use strict";
var a = "hello";
function foo() {
console.log(window.a); // hello
}
foo();
Method ํธ์ถ ์
- ๋ฉ์๋๋ ๊ฐ์ฒด(์ธ์คํด์ค)์ ๊ด๋ จ๋ ๋์์ ์๋ฏธํฉ๋๋ค.
- ๊ฐ์ฒด์ ์์์ธ ๋ฉ์๋์ this๋ ๊ทธ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
var a = "hello";
var obj = {
a: "world",
foo: function foo() {
console.log(this.a); // world
},
};
obj.foo(); // this๋ obj๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
var a = {
name: "a",
b: {
name: "b",
foo: function () {
console.log(this.name); // b
},
},
};
a.b.foo(); // this๋ a.b๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
๋ฉ์๋ ๋ด๋ถ ํจ์์์์ this ์ฐํ๋ฒ
var name = "lisa";
var obj = {
name: "jennie",
getName: function () {
console.log(this.name); // jennie (this === obj)
function getThisName() {
console.log(this.name); // lisa (this === window)
}
getThisName();
},
};
obj.getName();
์ ์์ค์์ getThisName
์ this๋ฅผ obj๋ฅผ ๊ฐ๋ฆฌํค๋๋ก(this === obj
) ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น?
๋ณ์์ this๋ฅผ ๋ด๋ ๋ฐฉ๋ฒ
- ๋ณ์์
this
๋ฅผ ๋ด์ obj๋ฅผ ๊ฐ๋ฆฌํฌ ์ ์์ต๋๋ค.
var name = "lisa";
var obj = {
name: "jennie",
getName: function () {
const newThis = this;
function getThisName() {
console.log(newThis.name); // jennie
}
getThisName();
},
};
obj.getName();
ํ์ดํ ํจ์ ์ฌ์ฉํ๊ธฐ
- ํ์ดํ ํจ์์๋ this๊ฐ ์์ต๋๋ค.
- ์ค์ฝํ ์ฒด์ด๋ ์์ this์ ์ ๊ทผํ์ฌ
obj
๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
var name = "lisa";
var obj = {
name: "jennie",
getName: function () {
console.log(this.name); // jennie
var getThisName = () => {
console.log(this.name); // jennie
};
getThisName();
},
};
obj.getName();
์ฝ๋ฐฑ ํธ์ถ ์ - ๋ช ์์ this ๋ฐ์ธ๋ฉ
call
func.call(thisArg[, arg1[, arg2[, ...]]])
var callback = function () {
console.log(this); // window
};
var obj = {
foo: function (cb) {
cb();
},
};
obj.foo(callback);
this
๋ฅผ obj
๋ฅผ ๊ฐ๋ฆฌํค๋ ค๊ณ ํ๋ค๋ฉด ์๋์ ๊ฐ์ด call
์ ์ฌ์ฉํ์ฌ this๋ฅผ ๋ช
์์ ์ผ๋ก ์ง์ ํ ์ ์์ต๋๋ค.
var callback = function () {
console.log(this); // obj
};
var obj = {
foo: function (cb) {
cb.call(this);
},
};
obj.foo(callback);
call์ ๊ฒฝ์ฐ arg ์ธ์์ ์๊ฐ ์ ํด์ ธ ์์ง ์์ต๋๋ค.
var string = "hello";
function foo(a, b, c, d, e) {
console.log(this.str);
console.log(arguments);
}
var obj = {
str: "world",
};
foo.call(obj, 1, 2, 3, 4, 5);
apply
func.apply(thisArg, [argsArray])
var string = "hello";
function foo(a, b, c, d, e) {
console.log(this.str);
console.log(arguments);
}
var obj = {
str: "world",
};
foo.apply(obj, [1, 2, 3, 4, 5]);
- ์ธ์๋ฅผ 2๊ฐ๋ง ๋ฐ์ต๋๋ค.
- ์ฒซ ๋ฒ์งธ๋ this๊ฐ์ ๋ฃ์ด์ฃผ๊ณ , ๋ ๋ฒ์งธ๋ ์ธ์๋ ๋ฐฐ์ด๋ก ๋ค์ด๊ฐ๋๋ค.
bind
this๊ฐ์ ์ค์ ํด ๋์ ํจ์๋ฅผ ์คํํ์ง ์๊ณ ํจ์ ์์ฒด๋ฅผ ๋ฐํํด์ค๋๋ค.
func.bind(thisArg[, arg1[, arg2[, ...]]])
var callback = function () {
console.log(this); // obj
};
var obj = {
foo: function (cb) {
cb.call(this);
},
};
setTimeout(callback, 1000);
์์ ๊ฐ์ด setTimeout
์ ์ฝ๋ฐฑ์ ๋ด์ ๋๊ธฐ๋ฉด this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.this
๋ฅผ obj
๋ฅผ ๊ฐ๋ฆฌํค๋ ค๊ณ ํ๋ค๋ฉด ์๋์ ๊ฐ์ด bind
๋ฅผ ์ฌ์ฉํ์ฌ this๋ฅผ ๋ช
์์ ์ผ๋ก ์ง์ ํ ์ ์์ต๋๋ค.
var callback = function () {
console.log(this); // obj
};
var obj = {
foo: function (cb) {
cb.call(this);
},
};
setTimeout(callback.bind(obj), 1000);
์์ฑ์ ํจ์ ํธ์ถ ์
new ์ฐ์ฐ์๋ฅผ ์ผ์ ๋ ์์ฑ์ ํจ์์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ธ์คํดํธ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ช ๋ น์ผ๋ก ์๋ก ๋ง๋ค ์ธ์คํด์ค ๊ฐ์ฒด ์์ฒด๊ฐ this๊ฐ ๋ฉ๋๋ค.
- ์์ฑ์(constructor)๋ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ์ญํ ์ ํ๋ ํจ์(class)์ ๋๋ค..
- ์์ฑ์ ํจ์๋ ์ฒซ ๊ธ์๋ฅผ ๋๋ฌธ์๋ก ํ๊ธฐํฉ๋๋ค.
- ํจ์ ์์ new๋ฅผ ๋ถ์ด๋ฉด ๋ฆฌํด ๊ฐ์ ๊ฐ์ฒด(instance)๊ฐ ๋๋ค.
function Foo() {
console.log(this); // {}
}
new Foo(); // ๊ฐ์ฒด instance
new
ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ํจ์๋ฅผ ํธ์ถํ๋ฉด ํจ์ ์์ this์ ๊ฐ์ ๋น ๊ฐ์ฒด
๊ฐ ํ ๋น๋์ด์ ํจ์๊ฐ ์คํ๋ฉ๋๋ค.
function Foo() {
this.name = "sara";
}
const a = new Foo();
์ ์์ค๋ ์๋์ ๊ฐ์ด this๋ฅผ return ํด์ฃผ๋ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค.
function Foo() {
this.name = "sara";
/*
{
name: 'sara';
}
return this;
*/
}
var a = new Foo();
var name = "sara";
var age = 30;
function Person(name, age) {
this.name = name;
this.age = age;
}
const lisa = Person("lisa", 20);
console.log(window.name, window.age); // lisa, 20
์ ์์ค์ฒ๋ผ new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ํธ์ถํ๊ฒ ๋๋ฉด this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๊ณ ์ ์ญ ๊ฐ์ฒด์ name, age์ ๊ฐ์ ์ฌํ ๋น ํ๊ฒ ๋ฉ๋๋ค.
var name = "sara";
var age = 30;
function Person(name, age) {
this.name = name;
this.age = age;
}
var lisa = new Person("lisa", 20);
console.log(window.name, window.age); // sara, 30
console.log(lisa);
new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ๊ฐ์ฒด๊ฐ ์๋ก ๋ง๋ค์ด์ง๋ฉด์ ๊ทธ ๊ฐ์ฒด ์์ name, age ํ๋กํผํฐ๊ฐ ์์ฑ๋๋ฉด์ lisa๋ผ๋ ๋ณ์์ ๋ด๊ธฐ๊ฒ ๋ฉ๋๋ค.
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Class ํด๋์ค (0) | 2022.12.23 |
---|---|
๊ฐ์ฒด ์งํฅ Prototype (0) | 2022.12.04 |
์คํ ์ปจํ ์คํธ (0) | 2022.11.13 |
๋ฐ์ดํฐ ํ์ (์์ํ vs ์ฐธ์กฐํ) (0) | 2022.11.01 |