์บกํดํ๊ต๋์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ๋ณธ ๋ค์ง๊ธฐ ๊ธ์ ์ฐธ์กฐํ์ฌ ๊ณต๋ถํ๋ฉฐ ๋์ ์ธ ๊ธฐ๋ก์ ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ํ์
<๊ธฐ๋ณธ ํ์ >
1. Number - ์ค์
2. String - ๋ฌธ์์ด
3. Boolean
4. undefined - ๋ณ์์ ๊ฐ์ด ํ ๋น๋๊ธฐ ์ ๊ฐ์ด์ ํ์
5. null - ๊ฐ๋ฐ์๊ฐ ์๋์ ์ผ๋ก ํ ๋นํ๋ ๊ฐ. ํ์ ์ Object์ผ๋ก ๋ฐํ
<์ฐธ์กฐ(๊ฐ์ฒด) ํ์ >
1. Object
2. Array
3. Function
NaN?
์ ์์ ๊ฐ์ ์ป์ง ๋ชปํ ๋ ๋ฐ์ํ๋ ์๋ฌ
์์) ํ๋กํผํฐ๋ช ์ด ์ฐ์ฐ์ ํฌํจํ์ฌ ์๋ฌ
delete ์ฐ์ฐ์
๊ฐ์ฒด ํ๋กํผํฐ ์ญ์ . ๋จ, ๊ฐ์ฒด ์ญ์ ๋ถ๊ฐ!
๊ฐ์ฒด์ ๋ชจ๋ ์ฐ์ฐ์ ์ฐธ์กฐ ๊ฐ์ผ๋ก ์ฒ๋ฆฌํจ
Array์ Object์ ๊ตฌ๋ถ
delete, splice ์ฐ์ฐ์์ ๋ฐฐ์ด
typeof
var num = 10;
var str = "a";
var boolean = true;
var obj = {};
var undefined;
var nullValue = null;
var arr = [];
function func() {};
console.log(typeof num); // number
console.log(typeof str); // string
console.log(typeof boolean); // boolean
console.log(typeof obj); // object
console.log(typeof undefined); // undefined
console.log(typeof nullValue); // object (null ์ object)
console.log(typeof arr); // object (๋ฐฐ์ด๋ object)
console.log(typeof func); // function
ํจ์ ํธ์ด์คํ
ํจ์๋ ๋จผ์ ํธ์ถํ๊ณ , ํจ์ ์ ์๋ ๋์ค์ ํ ์ ์๋ค.
์์ 1
function hoisting() {
hoistingText = "ํธ์ด์คํ
";
var hoistingText;
console.log(hoistingText);
}
hoisting(); //ํธ์ด์คํ
↓ 1๋ฒ์ ๋ํด ํธ์ด์คํ
์ผ๋ก ์ฌํด์๋ ์ฝ๋
function hoisting() {
var hoistingText;
hoistingText = "ํธ์ด์คํ
";
console.log(hoistingText);
}
์์ 2
function hoisting() {
console.log("1 : " + hoistingText);
var hoistingText = "ํธ์ด์คํ
";
console.log("2 : " + hoistingText);
}
hoisting();
//1 : undefined
//2 : ํธ์ด์คํ
์ 1๋ฒ console log์ undefined๊ฐ ๋ฐ๊น?
↓ 2๋ฒ์ ๋ํด ํธ์ด์คํ
์ผ๋ก ์ฌํด์๋ ์ฝ๋
function hoisting()
{
var hoistingText;
console.log("1 : + hoistingText);
hoistingText = "ํธ์ด์คํ
";
console.log("2 : + hoistingText);
}
hoisting();
ํธ์ด์คํ ์ผ๋ก var hoistingText ์ ์ธ์ด ์ต์์๋ก ์ฌ๋ผ์จ๋ค.
1๋ฒ console log๋ ์ ์ธ๋ง ํ ๋ณ์์ด๋ฏ๋ก, undefined๊ฐ ์ฐํ๋ค.
๋ด๋ถ ํจ์
์์ฑ์ ํจ์
์ผ๋ฐ ๊ฐ์ฒด ์ ์ธ๊ณผ๋ ๋ฌ๋ฆฌ, ์ฌ๋ฌ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ์ฐ์ด๋ผ ์ ์์
ํจ์๋ช ๋งจ ์ ๊ธ์๋ ๋๋ฌธ์!
instanceof ํ์ฉ ์์ฑ์ ํจ์ ๊ตฌ๋ถ
์๋ฐ์คํฌ๋ฆฝํธ๋ ์์ฑ์ ํจ์ ํ์์ด ๋ณ๋๋ก ์์. ๊ธฐ์กด ํจ์์ new๋ง ๋ถ์ฌ์ฃผ๋ฉด ์์ฑ์ ํจ์ ์์ฑ ๊ฐ๋ฅ
์์ฑ์ ํจ์๊ฐ ์๋๋ฐ, new๋ฅผ ๋ถ์ด๋ ๊ฒฝ์ฐ๋ฅผ ๋๋นํ๊ธฐ ์ํด ํ๋จ์ ๊ธฐ๋ฒ ์ฌ์ฉ.
๋๋ถ๋ถ์ ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ฌ์ฉํ๋ ํจํด
function Func(arg) {
if(!(this instanceof arguments.callee))
{
return new Func(arg);
this.value = arg || 0;
}
}
var a = new Func(100);
var b = Func(200);
console.log(a.value) //1000
console.log(b.value) //2000
Func์ด๋ผ๋ ํจ์๋ฅผ callํ๋ฉด, ์์ฑ์ ํจ์๋ฅผ ๋ง๋ค์ด์ฃผ๊ธฐ ์ํ ์ฝ๋
๋๋ถ๋ถ์ ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ฌ์ฉํ๋ ํจํด์ด๋ค.
ํ๋กํ ํ์ ์ฒด์ด๋
ํด๋น ํจ์์ ์กด์ฌํ์ง ์๋ ์์ฑ, ๋ฉ์๋๋ฅผ ๋ถ๋ชจ ๊ฐ์ฒด(ํ๋กํ ํ์
๊ฐ์ฒด)์์ ์ฐพ์
obj์ ํ๋กํ ํ์
๊ฐ์ฒด๊ฐ Object์ธ๋ฐ hasOwnProperty ๋ฉ์๋๋ Object์ ๋ด์ฅ ๋ฉ์๋์ด๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ๋ชจ๋ ๊ฐ์ฒด์ ์ต์์ ๋ถ๋ชจ ๊ฐ์ฒด๋ Object ๊ฐ์ฒด์ด๋ฏ๋ก!
var obj = {
name : 'kea',
printName : function() {
console.log(this.name);
}
};
obj.printName(); //kea
obj.hasOwnProperty('name'); //true
obj.hasOwnProperty('age'); //false
์ฆ์ ์คํ ํจ์
ํจ์๋ฅผ ์ ์ํจ๊ณผ ๋์์ ๋ฐ๋ก ์คํ
ํจ์๋ฅผ ๋ค์ ํธ์ถํ ์ ์๋ค. ์ต์ด ํ๋ฒ ์คํ๋ง ์๊ตฌ๋๋ ์ด๊ธฐํ ์ฝ๋์ ์ ํฉ.
๊ธฐ๋ณธํํ
(function() {
// statements
})()
โป ์ฌ์ฉ๋ฒ
1. ๊ธฐ๋ช ์ฆ์ ์คํ ํจ์ (๋ ์์๋ ๋์ผ)
(function a(x) {
console.log(x*x);
})(2);
(function a(x) {
console.log(x*x);
}(2));
2. ์ต๋ช ์ฆ์ ์คํ ํจ์
(function (x) {
console.log(x*x);
})(2);
(function (x) {
console.log(x*x);
}(2)));
โป ๋ณ์์ ์ฆ์ ์คํ ํจ์๋ฅผ ์ ์ฅํ ์๋ ์๋ค.
ํจ์๋ฅผ ๋ณ์ square์ ์ ์ฅํ๊ณ , ๋ฐ๋ก ์คํ ๊ฐ๋ฅ
๋ณ์์ ์ ์ฅํ์์ผ๋ฏ๋ก ์ฌํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
//์ฆ์ ์คํ ํจ์ ๋ณ์์ ์ ์ฅํ๋ฉด์ ์คํ
(square = function (x) {
console.log(x*x); //4
})(2);
//์ฌํธ์ถ
square(3); //9
โป ๋ณ์์ ์ฆ์ ์คํ ํจ์ ๋ฆฌํด ๊ฐ์ ์ ์ฅํ ์๋ ์๋ค.
var square = (function (x) {
return x*x;
})(2);
console.log(square); //4
Array.prototype.map()
๋ฐฐ์ด ๋ด์ ๋ชจ๋ ์์์ ๋ํด ์ฃผ์ด์ง ํจ์๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์, ์๋ก์ด ๋ฐฐ์ด ๋ฐํ
arr(map(callback(currentValue[, index[, array]])[, thisArg])
์์ 1
// definition
Array.prototype.myMap = function(callback) {
arr = [];
for (var i = 0; i < this.length; i++)
arr.push(callback(this[i], i, this));
return arr;
};
//tests
var arrs = ['dic tanin', 'boo radley', 'hans gruber'];
var numbers2 = [1, 4, 9];
var goodT = arrs.myMap(function(n) {
return n;
});
var squareRoot = numbers2.myMap(function(num) {
return Math.sqrt(num);
});
console.log(goodT); // [ 'dic tanin', 'boo radley', 'hans gruber' ]
console.log(squareRoot); // [ 1, 2, 3 ]
์์ 2
const arrayTest = [1,2,3,4];
const mapTest = arrayTest.map(x => x * 2);
console.log(mapTest); //[2,4,6,8]
์์ 3
var array = [{key:1, value:10},
{key:2, value:20},
{key:3, value:30}];
var reformattedArray = array.map(function(obj){
var rObj = {};
rObj[obj.key] = obj.value;
return rObj;
});
//reformattedArray๋ [{1:10}, {2:20}, {3:30}]
๋น๋๊ธฐ ์คํ ๋ฐฉ์์ ์ดํด
์์ 1
console.log("1");
function exec() {
setTimeout(function() {
console.log("2");
}, 3000);
setTimeout(function() {
console.log("3");
}, 0);
console.log("4");
setTimeout(function () {
console.log("5");
}, 1000);
}
console.log(exec()); //1,4,3,5,2
์์ 2
๊ฒฐ๊ณผ๊ฐ 0,1,2,3,4๊ฐ ์๋์ ์ฃผ์
for๋ฌธ์ ์คํ์ด ๋ชจ๋ ๋๋ ํ์, setTimeout์ ์ฝ๋ฐฑ์ด ์คํ๋๊ธฐ ๋๋ฌธ์ 5,5,5,5,5
var i;
for(i=0; i<5; i++) {
setTimeout(function() {
console.log(i); //5,5,5,5,5
}, 1000);
}
arguments
ํจ์ ํธ์ถ ์, ๋๊ฒจ์ง ์ค์ ์ธ์ ๊ฐ์ ๊ฐ์ง ๋ฐฐ์ด
* arguments๋ index๋ฅผ ์ง๋์ง๋ง ๋ฐฐ์ด์ด ์๋์ ์ฃผ์
์ด๋ฌํ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๊ณผ ๋น์ทํ ๊ฐ์ฒด(array-like object)๋ผ๊ณ ํ๋ค.
function add(a,b) {
console.dir(arguments);
}
console.log(add(1)); // Arguments(1), 0:1
console.log(add(1, 2)); // Arguments(2), 0:1, 1:2
console.log(add(1, 2, 3)); // Arguments(3), 0:1, 1:2, 2:3
function sum() {
for(var i=0, result=0; i<arguments.length; i++) {
result += arguments[i];
}
result result;
}
console.log(sum(1,2,3)); //6
console.log(sum(1,2,3,4,5,6)); //21
'๐ Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[javascript] ๋ฐฐ์ด ์ ๊ฐ์ฒด๋ค์ ๊ฐ ์์ ๋ํ๊ธฐ (0) | 2023.02.02 |
---|---|
[javascript] <input> ํ๊ทธ์ ์ซ์๋ง ์ ๋ ฅ (์ ๊ท์ ์ฌ์ฉ) (0) | 2023.01.27 |
[Javascript] ES5, 6 ์ฐจ์ด (0) | 2022.05.23 |
[Javascript] instanceof, arguments (0) | 2022.04.29 |
[Javascript] this (0) | 2022.03.05 |