this
μ μ
μλ°μ€ν¬λ¦½νΈμ thisλ μμ μ΄ μν κ°μ²΄ λλ μμ μ΄ μμ±ν μΈμ€ν΄μ€λ₯Ό κ°λ¦¬ν€λ μκΈ° μ°Έμ‘° λ³μμ΄λ€. λΌκ³ μ°μ¬μλ€. λ§μ΄ μ’ μ΄λ €μ΄λ° κ°λ¨ν μμλ₯Ό ν΅ν΄ μμ보μ.
let person = {
firstName: "Kim",
lastName: "Chungyeon",
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
console.log(person.fullName()); //Kim ChungYeon
μ΄ μμ μ½λλ this μ μ μ μ€ "μμ μ΄ μν κ°μ²΄λ₯Ό κ°λ¦¬ν€λ~"
μ λν μ½λμ΄λ€. μμμμ thisλperson κ°μ²΄μ fullName λ©μλμμ μ¬μ©λμλ€. thisλ μ€ν μ€μΈ ν¨μκ° μν κ°μ²΄λ₯Ό κ°λ¦¬ν€λ―λ‘ μ¬κΈ°μ thisλ λ°λ‘ person κ°μ²΄λ₯Ό κ°λ¦¬ν¨λ€.
function Person(name) {
this.name = name;
}
Person.prototype.introduce = function() {
console.log("μλ
νμΈμ, μ μ΄λ¦μ " + this.name + "μ
λλ€.");
}
// μΈμ€ν΄μ€ μμ±
let person1 = new Person("Kim");
let person2 = new Person("Lee");
person1.introduce(); // μλ
νμΈμ, μ μ΄λ¦μ Kimμ
λλ€.
person2.introduce(); //
μ΄λ²μλ "μμ μ΄ μμ±ν μΈμ€ν΄μ€λ₯Ό κ°λ¦¬ν€λ~"
μ λν μμ μ½λλ₯Ό 보μ.
μμμμ thisλ Person κ°μ²΄μ νλ‘ν νμ
μ μΆκ°λ λ©μλμΈ introduceμμ μ¬μ©λμλ€. μ¦, μ¬κΈ°μ λ§νλ "μμ "μ νλ‘ν νμ
μ λ§λ Person κ°μ²΄μ΄λ©°, Person κ°μ²΄κ° μμ±ν μΈμ€ν΄μ€κ° λ°λ‘ person1κ³Ό person2 μ΄λ€. λ°λΌμ thisλ Personμ΄ μμ±ν μΈμ€ν΄μ€μΈ person1κ³Ό person2λ₯Ό κ°λ¦¬ν€λ κ²
μ΄λ€.
λ λ²μ§Έ μμ μ½λμμ μμλ³Έ κ²μ²λΌ, thisλ νΈμΆνλ λμμ λ°λΌ λμ μΌλ‘ κ°λ¦¬ν€λ κ°μ΄ κ²°μ λλ©°, thisμ κ°μ΄ κ²°μ λλ κ²μ λ°μΈλ©μ΄λΌκ³ νλ€. λ°μΈλ©μ λν΄ μ‘°κΈ λ μ΄ν΄λ³΄μ
λ°μΈλ©
μΌλ° ν¨μμμ thisλ μ μ κ°μ²΄μΈ windowμ κΈ°λ³Έμ μΌλ‘ λ°μΈλ© λλ€. (node.jsμμλ global)
console.log(this) // window
function test() {
console.log(this); // Window {0: Window, ...}
function inner() {
console.log(this); // Window {0: Window, ...}
}
inner();
}
test();
λ©μλ νΈμΆ μμλ μ°λ¦¬κ° μμ μ½λ 1 μμ νμΈ νλ κ²μ²λΌ λ©μλλ₯Ό νΈμΆν κ°μ²΄μ λ°μΈλ© λλ€.
μ μν μ μ, λ§μ½ νμ΄ν ν¨μλ‘ μμ±μ΄ λ κ²½μ°, this λ μμ 컨ν μ€νΈμ thisλ₯Ό μ°Έμ‘°νκΈ° λλ¬Έμ μλ μμμμλ μ μ κ°μ²΄μ λ°μΈλ© λλ€.
μλμμ λ€μ ν λ² μ΄ν΄λ³΄μ.
const obj = {
normal: function() {
console.log('normal fn this:', this);
},
arrow: () => {
console.log('arrow fn this:', this);
},
}
obj.normal(); // normal fn this: obj
obj.arrow(); // arrow fn this: window
λ€μμΌλ‘ μμ±μ ν¨μμμλ μμ±μ ν¨μλ‘ λΆν° λ§λ€μ΄μ§λ μΈμ€ν΄μ€μ λ°μΈλ© λλ€.
function User(name) {
this.name = name;
}
const user1 = new User('Kim');
console.log(user1.name); // Kim
κ·Έλ°λ° λ§μ½ new
ν€μλλ₯Ό μ¬μ©νμ§ μμ μΌλ° ν¨μλ‘ νΈμΆλλ κ²½μ°, μ μκ°μ²΄λ‘ λ°μΈλ© λλ€.
μμμ μ μ μ΄ν΄λ³΄μλλ°, νμ΄νν¨μμμ μ¬μ©λ thisμ κ²½μ°, Lexical thisλ₯Ό λ°μΈλ©
νλ€. μ½κ² λ§ν΄, νμ΄ν ν¨μμ λ μ컬 μ€μ½νμ μ‘΄μ¬νλ thisλ₯Ό λ°μΈλ©
νκ² λλ€.
const arrow= () => {
console.log(this);
}
arrow(); // window
// arrow λ©μλμ μ€μ½νμ μ μλμμΌλ―λ‘, thisκ° arrow λ©μλμ thisλ₯Ό κ°λ¦¬ν¨λ€.
const obj = {
arrow: function() {
const arrowInner = () => {
console.log(this);
}
arrowInner ();
}
};
obj.arrow(); // obj
μμ μ½λλ₯Ό 보면 obj κ°μ²΄μ arrow λ©μλμ λ μ컬 μ€μ½νλ μμ±λμμ λΉμμ νκ²½μΈ obj κ°μ²΄μ΄κ³ , arrowInner λ νμ΄ν ν¨μμ΄λ―λ‘, thisλ arrow λ©μλμ μ€μ½νμ thisλ₯Ό κ°λ¦¬ν€κ² λλ€. κ·Έλ°λ° arrow λ©μλμ μ€μ½νμ thisκ° λ°λ‘ obj κ°μ²΄μ΄λ―λ‘ thisλ obj κ°μ²΄λ₯Ό κ°λ¦¬ν€λ κ²μ΄λ€.
λ§μ§λ§μΌλ‘ μ΄λ²€νΈ μ²λ¦¬μμ thisλ₯Ό μ΄ν΄λ³΄μ.
const button = document.getElementById('button');
const consoleThis = function (e) {
console.log(e.target === this, this);
};
button.addEventListener('click', consoleThis);
// <button id="button">click</button>
μ΄λ²€νΈ μ²λ¦¬μμ thisλ μ΄λ²€νΈλ₯Ό λ°μ HTML μμλ₯Ό κ°λ¦¬ν¨λ€. μμ μ½λμμλ button μ ν΄λ¦ μ΄λ²€νΈλ₯Ό 쀬기 λλ¬Έμ, console.log μ μΆλ ₯ λλ κ°, thisλ λ°λ‘ button μμ μ체μ΄λ€.
Last updated