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
  • Stack

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
  • Queue

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

    • concat()
    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
    • slice()
    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;

์ฐธ์กฐ