function a()
{
if(!(this instanceof arguments.callee))
{
return new a();
}
}
1. function
function๋ ํ๋์ ๊ฐ์ฒด์ด๋ค.
2. this, new
this๋ ํจ์์ ํธ์ถ์๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
์ฆ์์คํํจ์์ฒ๋ผ ์์ ํธ์ถ์๊ฐ ์๋ค๋ฉด, ๊ธฐ๋ณธ์ ์ผ๋ก window ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
ํจ์ ์ ์ธ ์, ์์ new๊ฐ ๋ถ๊ณ ์๋ถ๊ณ ๋ ์์ ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ถ๋ฅธ๋ค.
a(); //a ํจ์ ์ this๋ window ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
var func = a(); //a ํจ์ ์ this๋ window ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค. (์ฐธ๊ณ func; ๋ฅผ ํ๋ฉด, a()๊ฐ ์คํ๋๋ค.)
new a(); //a ํจ์ ์ this๋ ์๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
var func = new a(); //a ํจ์ ์ this๋ ์๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค. (์ฐธ๊ณ func; ๋ฅผ ํ๋ฉด, ์๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.)
//this๋ฅผ ๋ณ๊ฒฝํ๊ณ ์ถ๋ค๋ฉด? call / apply ํจ์๋ฅผ ์ฌ์ฉ
3. A instacneof B
์ ๋ค๋ก, ์ธ์๋ฅผ ๋ฐ์์ ๋น๊ตํ์ฌ boolean๊ฐ ๋ฐํ
Q. A๊ฐ B์ ์์์ธ๊ฐ?
Q. A๋ B์ prototype chain ํ์์ธ๊ฐ?
var Person = function()
{
this.name = "kimeuna"
this.age = "29"
};
var Kimeuna = new Person();
Kimeuna instacneof Person; //true
Kimeuns instacneof Object; //true
[1, 2] instanceof Array ; //true
{ a: "aa" } instanceof Object ; //true
true instanceof Boolean ; //false
"A" instanceof String ; //false
var str = new String("def");
var str2 = "abc";
str instanceof String; //true
str2 instanceof String; //false
4. arguments
arguments๋ function ๊ฐ์ฒด์ ๊ณ ์ ํ๋กํผํฐ
function myfunc(a,b,c)
{
console.log(arguments[0]);
return arguments;
}
myfunc(1,2,3) //1
/**
Arguments
0 : 1
1 : 2
2 : 3
* callee? ํจ์ ์๊ธฐ ์์ ์ ๋ฐํํด์ฃผ๋ ํญ๋ชฉ
*/
5. ํด์
if(!(this instanceof arguments.callee))
- a๋ผ๋ function์ ํธ์ถ์(this)๊ฐ ์ด ํจ์(arguments.callee)์ ์์์ธ์ง, prototype chain์ ์ํด์์ง ์์์ง ํ๋ณ
return new a();
- ์ํด์์ง ์๋ค๋ฉด, new a()๋ฅผ returnํด์ค๋ค.
โ
์ถ์ฒ
https://gogoonbuntu.tistory.com/46
'๐ 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] this (0) | 2022.03.05 |