Baseline Widely available
JavaScriptìì í¨ìì this
í¤ìëë ë¤ë¥¸ ì¸ì´ì ì¡°ê¸ ë¤ë¥´ê² ëìí©ëë¤. ëí ì격 모ëì ë¹ì격 모ëììë ì¼ë¶ ì°¨ì´ê° ììµëë¤.
ëë¶ë¶ì ê²½ì° this
ì ê°ì í¨ì를 í¸ì¶í ë°©ë²ì ìí´ ê²°ì ë©ëë¤. ì¤íì¤ìë í ë¹ì¼ë¡ ì¤ì í ì ìê³ í¨ì를 í¸ì¶í ë ë§ë¤ ë¤ë¥¼ ì ììµëë¤. ES5ë í¨ì를 ì´ë»ê² í¸ì¶íëì§ ìê´íì§ ìê³
this
ê°ì ì¤ì í ì ìëbind
ë©ìë를 ëì
íê³ , ES2015ë ì¤ì¤ë¡ì this
ë°ì¸ë©ì ì ê³µíì§ ìë íì´í í¨ì를 ì¶ê°íìµëë¤(ì´ë ë ì컬 컨í
ì¤í¸ìì this
ê°ì ì ì§í©ëë¤).
const test = {
prop: 42,
func: function () {
return this.prop;
},
};
console.log(test.func());
// Expected output: 42
구문 ê°
ì¤í 컨í ì¤í¸(global, function ëë eval)ì íë¡í¼í°ë ë¹ì격 모ëìì íì ê°ì²´ë¥¼ 참조íë©°, ì격 모ëììë ì´ë í ê°ì´ë ë ì ììµëë¤.
ì ì 문맥ì ì ì¤í ë§¥ë½ìì this
ë ì격 모ë ì¬ë¶ì ê´ê³ ìì´ ì ì ê°ì²´ë¥¼ 참조í©ëë¤.
// ì¹ ë¸ë¼ì°ì ììë window ê°ì²´ê° ì ì ê°ì²´
console.log(this === window); // true
a = 37;
console.log(window.a); // 37
this.b = "MDN";
console.log(window.b); // "MDN"
console.log(b); // "MDN"
í¨ì ë¬¸ë§¥ë ¸í¸: global
globalThis
íë¡í¼í°ë¥¼ ì¬ì©íì¬ ì½ëê° ì¤íì¤ì¸ íì¬ ì»¨í ì¤í¸ì ê´ê³ìì´ íì ì ì ê°ì²´ë¥¼ ì»ì ì ììµëë¤.
í¨ì ë´ë¶ìì this
ì ê°ì í¨ì를 í¸ì¶í ë°©ë²ì ìí´ ì¢ì°ë©ëë¤.
ë¤ì ìì ë ì격 모ëê° ìëë©° this
ì ê°ì´ í¸ì¶ì ìí´ ì¤ì ëì§ ìì¼ë¯ë¡, 기본ê°ì¼ë¡ ë¸ë¼ì°ì ììë window
ì¸ ì ì ê°ì²´ë¥¼ 참조í©ëë¤.
function f1() {
return this;
}
// ë¸ë¼ì°ì
f1() === window; // true
// Node.js
f1() === global; // true
ë°ë©´ì ì격 모ëìì this
ê°ì ì¤í 문맥ì ì§ì
íë©° ì¤ì ëë ê°ì ì ì§íë¯ë¡ ë¤ì ìììì ë³´ì¬ì§ë ê² ì²ë¼ this
ë undefined
ë¡ ë¨ìììµëë¤.
function f2() {
"use strict"; // ì격 모ë ì°¸ê³
return this;
}
f2() === undefined; // true
ëë²ì§¸ ìì ìì f2
를 ê°ì²´ì ë©ìëë ìì±(ì: window.f2()
)ì¼ë¡ì¨ê° ìë ì§ì í¸ì¶í기 ë문ì this
ë undefined
ì¬ì¼ í©ëë¤. ê·¸ë¬ë ì격 모ë를 ì²ì ì§ìí기 ììí ì´ê¸° ë¸ë¼ì°ì ììë 구ííì§ ììê³ , window
ê°ì²´ë¥¼ ì못 ë°ííìµëë¤.
this
ì ê°ì í 문맥ìì ë¤ë¥¸ 문맥ì¼ë¡ ëê¸°ë ¤ë©´ ë¤ì ììì ê°ì´ call()
ì´ë apply()
를 ì¬ì©íì¸ì.
// call ëë applyì 첫 ë²ì§¸ ì¸ìë¡ ê°ì²´ê° ì ë¬ë ì ìì¼ë©° thisê° ê·¸ ê°ì²´ì 묶ì
var obj = { a: "Custom" };
// ë³ì를 ì ì¸íê³ ë³ìì íë¡í¼í°ë¡ ì ì window를 í ë¹
var a = "Global";
function whatsThis() {
return this.a; // í¨ì í¸ì¶ ë°©ìì ë°ë¼ ê°ì´ ë¬ë¼ì§
}
whatsThis(); // thisë 'Global'. í¨ì ë´ìì ì¤ì ëì§ ììì¼ë¯ë¡ global/window ê°ì²´ë¡ ì´ê¸°ê°ì ì¤ì íë¤.
whatsThis.call(obj); // thisë 'Custom'. í¨ì ë´ìì objë¡ ì¤ì íë¤.
whatsThis.apply(obj); // thisë 'Custom'. í¨ì ë´ìì objë¡ ì¤ì íë¤.
ìì 2
function add(c, d) {
return this.a + this.b + c + d;
}
var o = { a: 1, b: 3 };
// 첫 ë²ì§¸ ì¸ìë 'this'ë¡ ì¬ì©í ê°ì²´ì´ê³ ,
// ì´ì´ì§ë ì¸ìë¤ì í¨ì í¸ì¶ìì ì¸ìë¡ ì ë¬ëë¤.
add.call(o, 5, 7); // 16
// 첫 ë²ì§¸ ì¸ìë 'this'ë¡ ì¬ì©í ê°ì²´ì´ê³ ,
// ë ë²ì§¸ ì¸ìë í¨ì í¸ì¶ìì ì¸ìë¡ ì¬ì©ë 멤ë²ë¤ì´ ìì¹í ë°°ì´ì´ë¤.
add.apply(o, [10, 20]); // 34
ë¹ì격 모ëìì this
ë¡ ì ë¬ë ê°ì´ ê°ì²´ê° ìë ê²½ì°, call
ê³¼ apply
ë ì´ë¥¼ ê°ì²´ë¡ ë³íí기 ìí ìë를 í©ëë¤. null
ê³¼ undefined
ê°ì ì ì ê°ì²´ê° ë©ëë¤. 7
ì´ë 'foo'
ì ê°ì ììê°ì ê´ë ¨ë ìì±ì를 ì¬ì©í´ ê°ì²´ë¡ ë³íëë©°, ë°ë¼ì ìì ì«ì 7
ì new Number(7)
ì ìí´ ê°ì²´ë¡ ë³íëê³ ë¬¸ìì´ 'foo'
ë new String('foo')
ì ìí´ ê°ì²´ë¡ ë³íë©ëë¤.
function bar() {
console.log(Object.prototype.toString.call(this));
}
bar.call(7); // [object Number]
bar.call("foo"); // [object String]
bar.call(undefined); // [object global]
bind
ë©ìë
ECMAScript 5ë Function.prototype.bind
를 ëì
íìµëë¤. f.bind(someObject)
를 í¸ì¶íë©´ f
ì ê°ì 본문(ì½ë)ê³¼ ë²ì를 ê°ì¡ì§ë§ thisë ì본 í¨ì를 ê°ì§ ìë¡ì´ í¨ì를 ìì±í©ëë¤. ì í¨ìì this
ë í¸ì¶ ë°©ìê³¼ ìê´ìì´ ì구ì ì¼ë¡bind()
ì 첫 ë²ì§¸ 매ê°ë³ìë¡ ê³ ì ë©ëë¤.
function f() {
return this.a;
}
var g = f.bind({ a: "azerty" });
console.log(g()); // azerty
var h = g.bind({ a: "yoo" }); // bindë í ë²ë§ ëìí¨!
console.log(h()); // azerty
var o = { a: 37, f: f, g: g, h: h };
console.log(o.a, o.f(), o.g(), o.h()); // 37, 37, azerty, azerty
íì´í í¨ì
íì´í í¨ììì this
ë ìì ì ê°ì¼ ì ì ë²ìì
ëë¤. ì ì ì½ëììë ì ì ê°ì²´ë¥¼ ê°ë¦¬íµëë¤.
var globalObject = this;
var foo = () => this;
console.log(foo() === globalObject); // true
ì°¸ê³ : íì´í í¨ì를 call()
, bind()
, apply()
를 ì¬ì©í´ í¸ì¶í ë this
ì ê°ì ì í´ì£¼ëë¼ë 무ìí©ëë¤. ì¬ì©í 매ê°ë³ì를 ì í´ì£¼ë ê±´ 문ì ìì§ë§, 첫 ë²ì§¸ 매ê°ë³ì(thisArg
)ë null
ì ì§ì í´ì¼ í©ëë¤.
// ê°ì²´ë¡ì ë©ìë í¸ì¶
var obj = { func: foo };
console.log(obj.func() === globalObject); // true
// callì ì¬ì©í this ì¤ì ìë
console.log(foo.call(obj) === globalObject); // true
// bind를 ì¬ì©í this ì¤ì ìë
foo = foo.bind(obj);
console.log(foo() === globalObject); // true
ì´ë¤ ë°©ë²ì ì¬ì©íë foo
ì this
ë ìì± ìì ì ê²ì¼ë¡ ì¤ì ë©ëë¤(ì ììììë global ê°ì²´). ë¤ë¥¸ í¨ì ë´ìì ìì±ë íì´í í¨ììë ëì¼íê² ì ì©ë©ëë¤. this
ë ì¸ì¬ì§ ë ì컬 컨í
ì¤í¸ì ê²ì¼ë¡ ì ì§ë©ëë¤.
// this를 ë°ííë ë©ìë bar를 ê°ë obj를 ìì±í©ëë¤.
// ë°íë í¨ìë íì´í í¨ìë¡ ìì±ëìì¼ë¯ë¡,
// thisë ê°ì¸ì§ í¨ìì thisë¡ ì구ì ì¼ë¡ 묶ì
ëë¤.
// barì ê°ì í¸ì¶ìì ì¤ì ë ì ìì¼ë©°, ì´ë ë°íë í¨ìì ê°ì ì¤ì íë ê²ì
ëë¤.
var obj = {
bar: function () {
var x = () => this;
return x;
},
};
// objì ë©ìëë¡ì¨ bar를 í¸ì¶íê³ , this를 objë¡ ì¤ì
// ë°íë í¨ìë¡ì 참조를 fnì í ë¹
var fn = obj.bar();
// this ì¤ì ìì´ fnì í¸ì¶íë©´,
// 기본ê°ì¼ë¡ global ê°ì²´ ëë ì격 모ëììë undefined
console.log(fn() === obj); // true
// í¸ì¶ ìì´ objì ë©ìë를 참조íë¤ë©´ 주ìíì¸ì.
var fn2 = obj.bar;
// íì´í í¨ìì this를 bar ë©ìë ë´ë¶ìì í¸ì¶íë©´
// fn2ì this를 ë°ë¥´ë¯ë¡ window를 ë°íí ê²ì
ëë¤.
console.log(fn2()() == window); // true
ì ìììì, obj.bar
ì í ë¹ë í¨ì(ìµëª
í¨ì Aë¼ê³ ì§ì¹)ë íì´í í¨ìë¡ ìì±ë ë¤ë¥¸ í¨ì(ìµëª
í¨ì Bë¼ê³ ì§ì¹)를 ë°íí©ëë¤. ê²°ê³¼ë¡ì¨ í¨ì Bê° í¸ì¶ë ë Bì this
ë ì구ì ì¼ë¡ obj.bar
(í¨ì A)ì this
ë¡ ì¤ì ë©ëë¤. ë°íë¨ í¨ì(í¨ì B)ê° í¸ì¶ë ë, this
ë íì ì´ê¸°ì ì¤ì ë ê°ì¼ ê²ì
ëë¤. ì ì½ë ìììì, í¨ì Bì this
ë í¨ì Aì this
ì¸ obj
ë¡ ì¤ì ëë¯ë¡, ì¼ë°ì ì¼ë¡ this
를 undefined
ë global ê°ì²´ë¡ ì¤ì íë ë°©ìì¼ë¡ í¸ì¶í ëë(ëë ì´ì ììììì²ë¼ global ì¤í 컨í
ì¤í¸ìì ë¤ë¥¸ ë°©ë²ì ì¬ì©í ëìë) objì ì¤ì ì ì ì§ë©ëë¤.
í¨ì를 ì´ë¤ ê°ì²´ì ë©ìëë¡ í¸ì¶íë©´ this
ì ê°ì ê·¸ ê°ì²´ë¥¼ ì¬ì©í©ëë¤.
ë¤ì ìì ìì o.f()
를 ì¤íí ë o
ê°ì²´ê° í¨ì ë´ë¶ì this
ì ì°ê²°ë©ëë¤.
var o = {
prop: 37,
f: function () {
return this.prop;
},
};
console.log(o.f()); // 37
ì´ íëì´ í¨ìê° ì ìë ë°©ë²ì´ë ìì¹ì ì í ìí¥ì ë°ì§ ìë ê²ì ì ìí´ë¼. ì´ì ìì ìì, o
ì ì ì ì¤ f
í¨ì를 구ì±ìì¼ë¡ ë´ë¶ì ì ì íìë¤. ê·¸ë¬ë, ê°ë¨íê² í¨ì를 먼ì ì ìíê³ ëì¤ì o.f
를 ì¶ê°í ì ìë¤. ì´ë ê² íë©´ ëì¼í ëì ê²°ê³¼ ì´ë¤ :
var o = { prop: 37 };
function independent() {
return this.prop;
}
o.f = independent;
console.log(o.f()); // logs 37
ì´ë í¨ìê° o
ì ë©¤ë² f
ë¡ ë¶í° í¸ì¶ ë ê²ë§ì´ ì¤ìíë¤ë ê²ì ë³´ì¬ì¤ë¤.
ë§ì°¬ê°ì§ë¡, ì´ this
ë°ì¸ë©ì ê°ì¥ ì¦ê°ì¼ë¡ ë©¤ë² ëìì ìí¥ì ë°ëë¤. ë¤ì ìì ìì, í¨ì를 ì¤íí ë, ê°ì²´ o.b
ì ë©ìë g
ë¡ì í¸ì¶íë¤. ì´ê²ì´ ì¤íëë ëì, í¨ì ë´ë¶ì this
ë o.b
를 ëíë¸ë¤. ê°ì²´ë ê·¸ ìì ì´ o
ì ë©¤ë² ì¤ íëë¼ë ì¬ì¤ì ì¤ìíì§ ìë¤. ê°ì¥ ì¦ê°ì ì¸ ì°¸ì¡°ê° ì¤ìí ê²ì´ë¤.
o.b = { g: independent, prop: 42 };
console.log(o.b.g()); // logs 42
ê°ì²´ì íë¡í íì
ì²´ì¸ììì this
ê°ì ê°ë
ì¼ë¡ ê°ì²´ì íë¡í íì
ì²´ì¸ ì´ëê°ì ì ìí ë©ìëë ë§ì°¬ê°ì§ì
ëë¤. ë©ìëê° ì´ë¤ ê°ì²´ì íë¡í íì
ì²´ì¸ ìì ì¡´ì¬íë©´, this
ì ê°ì ê·¸ ê°ì²´ê° ë©ìë를 ê°ì§ ê² ë§ë¥ ì¤ì ë©ëë¤.
var o = {
f: function () {
return this.a + this.b;
},
};
var p = Object.create(o);
p.a = 1;
p.b = 4;
console.log(p.f()); // 5
ì´ ìì ìì, f
ìì±ì ê°ì§ê³ ìì§ ìì ë³ì p
ê° í ë¹ë ê°ì²´ë, íë¡í íì
ì¼ë¡ ë¶í° ììë°ëë¤. ê·¸ë¬ë ê·¸ê²ì ê²°êµ o
ìì f
ì´ë¦ì ê°ì§ 멤ë²ë¥¼ ì°¾ë ëë 문ì ê° ëì§ ìëë¤; p.f
를 ì°¾ì 참조í기 ììíë¯ë¡, í¨ì ë´ë¶ì this
ë p
ì²ë¼ ëíëë ê°ì²´ ê°ì ì·¨íë¤. ì¦, f
ë p
ì ë©ìëë¡ì í¸ì¶ë ì´íë¡, this
ë p
를 ëíë¸ë¤. ì´ê²ì JavaScript ì íë¡í íì
ììì í¥ë¯¸ë¡ì´ 기ë¥ì´ë¤.
this
ë¤ì í ë² ê°ì ê°ë
ì¼ë¡, í¨ì를 ì ê·¼ìì ì¤ì ììì í¸ì¶íëë¼ë ëì¼í©ëë¤. ì ê·¼ìë ì¤ì ìë¡ ì¬ì©íë í¨ìì this
ë ì ê·¼íê±°ë ì¤ì íë ìì±ì ê°ì§ ê°ì²´ë¡ 묶ì
ëë¤.
function sum() {
return this.a + this.b + this.c;
}
var o = {
a: 1,
b: 2,
c: 3,
get average() {
return (this.a + this.b + this.c) / 3;
},
};
Object.defineProperty(o, "sum", {
get: sum,
enumerable: true,
configurable: true,
});
console.log(o.average, o.sum); // 2, 6
ìì±ìë¡ì
í¨ì를 new
í¤ìëì í¨ê» ìì±ìë¡ ì¬ì©íë©´ this
ë ìë¡ ì긴 ê°ì²´ì 묶ì
ëë¤.
While the default for a constructor is to return the object referenced by this
, it can instead return some other object (if the return value isn't an object, then the this
object is returned).
/*
* Constructors work like this:
*
* function MyConstructor(){
* // Actual function body code goes here.
* // Create properties on |this| as
* // desired by assigning to them. E.g.,
* this.fum = "nom";
* // et cetera...
*
* // If the function has a return statement that
* // returns an object, that object will be the
* // result of the |new| expression. Otherwise,
* // the result of the expression is the object
* // currently bound to |this|
* // (i.e., the common case most usually seen).
* }
*/
function C() {
this.a = 37;
}
var o = new C();
console.log(o.a); // 37
function C2() {
this.a = 37;
return { a: 38 };
}
o = new C2();
console.log(o.a); // 38
DOM ì´ë²¤í¸ ì²ë¦¬ê¸°ë¡ì
í¨ì를 ì´ë²¤í¸ ì²ë¦¬ê¸°ë¡ ì¬ì©íë©´ thisë ì´ë²¤í¸ë¥¼ ë°ì¬í ììë¡ ì¤ì ë©ëë¤. ì¼ë¶ ë¸ë¼ì°ì ë addEventListener()
ì¸ì ë¤ë¥¸ ë°©ë²ì¼ë¡ ì¶ê°í ì²ë¦¬ê¸°ì ëí´ì ì´ ê·ì¹ì ë°ë¥´ì§ ììµëë¤.
// ì²ë¦¬ê¸°ë¡ í¸ì¶íë©´ ê´ë ¨ ê°ì²´ë¥¼ íëê² ë§ë¦
function bluify(e) {
// ì¸ì ë true
console.log(this === e.currentTarget);
// currentTargetê³¼ targetì´ ê°ì ê°ì²´ë©´ true
console.log(this === e.target);
this.style.backgroundColor = "#A5D9F3";
}
// 문ì ë´ ëª¨ë ììì 목ë¡
var elements = document.getElementsByTagName("*");
// ì´ë¤ ìì를 í´ë¦íë©´ íëê² ë³íëë¡
// bluify를 í´ë¦ ì²ë¦¬ê¸°ë¡ ë±ë¡
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", bluify, false);
}
ì¸ë¼ì¸ ì´ë²¤í¸ í¸ë¤ë¬ìì
ì½ë를 ì¸ë¼ì¸ ì´ë²¤í¸ ì²ë¦¬ê¸°ë¡ ì¬ì©íë©´ this
ë ì²ë¦¬ê¸°ë¥¼ ë°°ì¹í DOM ììë¡ ì¤ì ë©ëë¤.
<button onclick="alert(this.tagName.toLowerCase());">this íì</button>
ìì ê²½ê³ ì°½ì button
ì ë³´ì¬ì¤ëë¤. ë¤ë§ ë°ê¹¥ìª½ ì½ëë§ this
를 ì´ë° ë°©ìì¼ë¡ ì¤ì í©ëë¤.
<button onclick="alert((function() { return this; })());">
ë´ë¶ this íì
</button>
ìì ê²½ì°, ë´ë¶ í¨ìì thisë ì í´ì§ì§ ììì¼ë¯ë¡ ì ì/window
ê°ì²´ë¥¼ ë°íí©ëë¤. ì¦ ë¹ì격 모ëìì this
를 ì¤ì íì§ ìì ê²½ì°ì 기본ê°ì
ëë¤.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4