-
Notifications
You must be signed in to change notification settings - Fork 0
javascript_jquery_basic_3_3b
์๋ฐ์คํฌ๋ฆฝํธ์์ this ๊ฐ์ฒด๋ ํจ์ ํธ์ถ์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ค.
this ๊ฐ์ฒด๊ฐ ์ค์ ๋๋ 3๊ฐ์ง ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ๋ค.
- someThing.someFunction(arg1, arg2, argN)
- someFunction.call(someThing, arg1, arg2, argN)
- someFunction.apply(someThing, [arg1, arg2, argN])
์ ์ํฉ์์ ๋ชจ๋ this๋ someThing์ด๋ค.
ํจ์๋ฅผ ํธ์ถํ ๋ ์ ํ ๋ถ๋ชจ๊ฐ ์๋ ๊ฒฝ์ฐ์๋, ์ผ๋ฐ์ ์ผ๋ก ๋ธ๋ผ์ฐ์ ์ global object์ธ window ๊ฐ์ฒด๊ฐ ๋ถ๋ชจ๊ฐ ๋๋ค.
var std_obj = {
options : { rows: 0, cols: 0 },
activeEffect : "none",
displayMe : function() {
// the 'this' pointer is referring to the std_obj
if (this.activeEffect=="fade") { }
var doSomeEffects = function() {
// the 'this' pointer is referring to the window obj, why?
if (this.activeEffect=="fade") { }
}
doSomeEffects();
}
};
std_obj.displayMe();์ ์ฝ๋์์ doSomeEffects()๋ ์ค์ ๋ก doSomeEffects.call(undefined);๋ก ํธ์ถ๋๋ค.
๋ฐ๋ผ์ doSomeEffects์ ์ค์ฝํ ๋ด๋ถ์ this๋ window ๊ฐ์ฒด๊ฐ ๋๋ค.
Javascript โthisโ pointer within nested function
ECMAScript ๊ท๊ฒฉ์์ ์ผ๋ฐ ํจ์ํธ์ถ์ this ๋ฐ์ธ๋ฉ์ ๋ค์๊ณผ ๊ฐ์ด ๋์ํ๋ค.
strict mode์ผ ๊ฒฝ์ฐ thisValue(this)๋ฅผ thisArgument(์จ์ ์ธ์)๋ก ์ง์ ํ๋คstrict mode๊ฐ ์๋๊ณ , thisArguments(์จ์ ์ธ์)๋ null ๋๋ undefined ๋ผ๋ฉด,- thisValue(this)๋ฅผ
[[globalThis]]๋ก ์ค์ ํ๋ค. // ์ผ๋ฐ์ ์ผ๋ก window ๊ฐ์ฒด- thisArguments๊ฐ null ๋๋ undefined๊ฐ ์๋๋ผ๋ฉด thisValue๋ฅผ thisArgument๋ก ์ค์ ํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ 1.2๋ถํฐ ๊ฐ์ฒด ์ด๋์
๋ผ์ด์ ๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๊ฒ ๋์๋ค.
๊ฐ์ฒด ์ด๋์
๋ผ์ด์ ๋ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํ ์ถ์ฝํ๊ธฐ๋ฒ์ด๋ค.
๊ฐ์ฒด ์ด๋์ ๋ผ์ด์ ๋ ์ด์ฉํ ๊ฐ์ฒด ์์ฑ ๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
var o = {
r: 'some value',
t: 'some other value'
};์ถ์ฝํ๊ธฐ๋ฒ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ฉด ๊ธฐ๋ฅ์ ์ผ๋ก ์๋์ ๊ฐ์ด ๊ธฐ๋ฅ์ ์ผ๋ก ๋์ผํ ์์ ์ ์ํํ๋ค.
var o = new Object();
o.r = 'some value';
o.t = 'some other value';What does ':' (colon) do in JavaScript?
Working with objects
Object initializer