Javascript
5. ์ฐธ์กฐ ํ์
-
๊ฐ์ฒด ๋ฆฌํฐ๋ด ํ๊ธฐ๋ฒ
-
Property๊ฐ ์ฌ๋ฌ ๊ฐ ์ธ ๋ ๊ฐ๋
์ฑ์ ํ๋ณดํ๋ ์ฉ๋๋ก ์ฌ์ฉ
var person = {
name : "NESOY",
age : 29
}
-
Array ํ์
-
๋ฐฐ์ด ๋ฆฌํฐ๋ด์ Array ์์ฑ์๋ฅผ ํธ์ถ X
-
๋ฐฐ์ด ๊ฐ์ง ํจ์ Array.isArray(๋ฐฐ์ด);
//Array ์์ฑ์
var colors = new Array();
var colors = new Array(20); // Length => 20
//๋ฐฐ์ด ๋ฆฌํฐ๋ด
var colors = [];
var colors = ["red","blue","black"];
//Array.isArray();
var result = Array.isArray(colors); // true;
-
๋ณํ ๋ฉ์๋
-
toString(), toLocaleString(), valueOf()
var colors = ["red","blue","green"];
alert(colors.toString()); // red,blue,green
alert(colors.toLocaleString()); // red,blue,green
alert(colors); // red,blue,green
-
toLocaleString()์ toString(), valueOf()์ ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ผ ์ ๋ ์๋ค.
var person1 = {
toLocaleString : function{
return "NESOY";
},
toString : function{
return "Young Jae";
}
};
-
join()์ ํตํด ๊ตฌ๋ถ์๋ฅผ ์ง์ ํ ์ ์๋ค.
var colors = ["Red","blue","green"];
alert(colors.join(",")); red,blue,green
alert(colors.join("||")); red||blue||green
var colors = new Array(); // ๋ฐฐ์ด ์์ฑ
var count = colors.push("red","green"); // ์๊น 2๊ฐ ์ถ๊ฐ
alert(count); // 2
count = colors.push("black");
alert(count); // 3
var item = colors.pop(); // item ๊บผ๋
alert(item); // black
alert(colors.length); //2
var colors = new Array(); // ๋ฐฐ์ด ์์ฑ
var count = colors.push("red","green"); // ์๊น 2๊ฐ ์ถ๊ฐ
alert(count); // 2
count = colors.push("black");
alert(count); // 3
var item = colors.shift(); // item ๊บผ๋
alert(item); // red
alert(colors.length); //2
-
Sort
-
reverse()์ sort()๋ ๋ชจ๋ ์์ ์ ํธ์ถํ ๋ฐฐ์ด์ ๋ํ ์ฐธ์กฐ๋ฅผ ๋ฐํํ๋ค. ์ฆ ์ฒด์ธํํ๋ก ์ฌ์ฉ๊ฐ๋ฅ ex) array.sort(compare).reverse();
var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values); // 5,4,3,2,1
var values = [5, 3, 4, 1, 2];
values.sort();
alert(values); // 1,2,3,4,5
-
์กฐ์ Method
var colors = ["Red","blue","green"];
var colors2 = colors.concat("yellow",["black,"brown"]);
alert(colors); // Red,blue,green
alert(colors2); // Red,blue,green,yellow,black,brown
var colors = ["Red","blue","green","yellow","black"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);
alert(colors2); // blue,green,yellow,black
alert(colors3); // blue,green,yellow
-
splice() = ์ฝ์
, ์ญ์ , ๋์ฒด ๊ฐ๋ฅ (startIndex, deleteCount, item1,โฆ)
var colors = ["red","green","blue"];
var removed = colors.splice(0,1); // ์ฒซ ๋ฒ์งธ ๋ฐ์ดํฐ ์ ๊ฑฐ
alert(colors); // green,blue
alert(removed); // red
removed = colors.splice(1, 0, "yellow", "orange"); // ์ธ๋ฑ์ค 1์ ๋ฐ์ดํฐ 2๊ฐ ์ถ๊ฐ
alert(colors); // green, yellow, orange, blue
alert(removed); // ๋น ๋ฐฐ์ด
removed = colors.splice(1, 1, "red", "purple"); //๋ฐ์ดํฐ2๊ฐ์ถ๊ฐ 1๊ฐ ์ ๊ฑฐ
alert(colors); // green,red,purple,orange,blue
alert(removed); // yellow
-
์์น Method
-
indexOf(), lastIndexOf() = str.indexOf(searchValue[, fromIndex])
'Blue Whale'.indexOf('Blue'); // returns 0
'Blue Whale'.indexOf('Blute'); // returns -1
'Blue Whale'.indexOf('Whale', 0); // returns 5
'Blue Whale'.indexOf('Whale', 5); // returns 5
'Blue Whale'.indexOf('', 9); // returns 9
'Blue Whale'.indexOf('', 10); // returns 10
'Blue Whale'.indexOf('', 11); // ์ ์ฒด ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 10์ด๋ฏ๋ก, 10์ ๋ฐํ
-
๋ฐ๋ณต Method
-
every() : ๋ฐฐ์ด์ ๋ชจ๋ ๋ฐ์ดํฐ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ๊ณ ๊ฐ์ด ์ ๋ถ true์ด๋ฉด true ๋ฐํ
-
filter() : ๋ฐฐ์ด์ ๋ชจ๋ ๋ฐ์ดํฐ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ๊ณ ๋ฐํ ๊ฐ์ด true์ธ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฐฐ์ด์ ์ ์ฅํ์ฌ ๋ฐํ
-
some() : ๋ฐฐ์ด์ ๋ชจ๋ ๋ฐ์ดํฐ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ๊ณ ๊ฐ์ด ํ๋๊ฐ true์ด๋ฉด true ๋ฐํ
-
forEach() : ๋ฐฐ์ด์ ๋ชจ๋ ๋ฐ์ดํฐ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ๊ณ ๋ฐํ๊ฐ ์์
-
map() : ๋ฐฐ์ด์ ๋ชจ๋ ๋ฐ์ดํฐ์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ๊ณ ์ ๋ฐฐ์ด์ ์ ์ฅํ์ฌ ๋ฐํ
var numbers = [1,2,3,4,5,4,3,2,1];
var everyResult = numbers.every(function(item, index, array){
return (item > 2);
})
alert(everyResult); // false
var someResult = numbers.some(function(item, index, array){
return (item > 2);
})
alert(someResult); // true
var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
})
alert(filterResult); // 3,4,5,4,3
var mapResult = numbers.map(function(item, index, array){
return item * 2;
})
alert(mapResult); // 2,4,6,8,10,8,6,4,2
-
Regular Expression
-
ํจํด์ ์ฐพ๋๋ฐ ์ ๊ทํํ์์ ์ฌ์ฉํ๋ฉด ๊ฐํธํ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค.
var myRegExp = /regexr/i;

์ฐธ์กฐ