๐Ÿช„ Set

๊ฐ์ฒด์ž๋ฃŒํ˜•
๊ฐ์ฒด ์•ˆ์˜ ๊ฐ’์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค -> ์ง‘ํ•ฉ ๊ฐœ๋…

let s = new Set('abcdeeeeeeeee');
s.size;

// Set์— ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ
s.add('f');

// Set ์ˆœํ™˜ํ•˜๊ธฐ
for (let variable of s) {
  console.log(variable);
}

// ๊ฐ’์ด ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ
let ss = new Set('abcdeeeeeeeee'.split(''));

// Set์˜ ๊ฐ’ ์ œ๊ฑฐํ•˜๊ธฐ
ss.delete('b');

// Set์˜ ๊ฐ’ ํ™•์ธํ•˜๊ธฐ
ss.has('a');

// Set์˜ ๋ชจ๋“  ๊ฐ’ ์ œ๊ฑฐํ•˜๊ธฐ 
ss.clear;

ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ

let a = new Set('abc');
let b = new Set('cde');
// ํ•ฉ์ง‘ํ•ฉ
let union = new Set([...a].concat(...b));
// ๊ต์ง‘ํ•ฉ
let cro = [...a].filter(value => b.has(value));
// ์ฐจ์ง‘ํ•ฉ
let dif = [...a].filter(x => !b.has(x));

NaN๊ณผ undefined๋„ Set์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์›๋ž˜ NaN !== NaN์ด์ง€๋งŒ, Set์—์„œ๋Š” NaN == NaN์œผ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค.

let test = ["์œค๋ง๋ž‘", NaN, NaN, NaN];
let s = new Set(test);
console.log(s); // Set(2) {'์œค๋ง๋ž‘', NaN}

forEach(): array์™€ ๋‹ค๋ฅด๊ฒŒ value๊ฐ€ 2๋ฒˆ ๋ฐ˜๋ณต๋œ๋‹ค

let s = new Set("aabbb");

// IE10์€ ๋ฏธ์ง€์›
// s.forEach((value, value, set) => {});
s.forEach((a, b, set) => {
  console.log(a, b, set);
});
// a a Set(2) {'a', 'b'}
// b b Set(2) {'a', 'b'}

๐Ÿช„ this

์ž์‹ ์„ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด or ์ž์‹ ์ด ์ƒ์„ฑํ•  ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜

  • ์ „์—ญ๊ณต๊ฐ„์˜ this๋Š” window(node๋Š” global)
function a() {
  console.log(this);
}

a(); 		// Window ํ˜ธ์ถœ
window.a();	// Window ํ˜ธ์ถœ
  • ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ this๋Š” ๋ฉค๋ฒ„์ ‘๊ทผ์—ฐ์‚ฐ์ž ์•ž์˜ ๊ฐ์ฒด
let myObj = {
  val1: 100,
  func1: function () {
    console.log(this);
  },
};

myObj.func1(); // myObj ํ˜ธ์ถœ

let test = myObj.func1;
test(); // Window ํ˜ธ์ถœ
  • ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ this๋Š” window(node๋Š” global)
const person = {
  name: "mallang",
  age: 23,
  a() {
    console.log(this);
    console.log(this.name);
    function b() {
      console.log(this);
      console.log(this.name);
      function c() {
        console.log(this);
        console.log(this.name);
      }
      c();
      // Window
      // 
    }
    b();
    // Window
    // 
  },
};
person.a();
// {name: 'mallang', age: 23, a: ฦ’}
// mallang
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ this๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„
    this์— ๋ฐ”์ธ๋”ฉ๋˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋˜๋Š” ๊ธฐ๋ช…ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌย ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค(Lexical this)
const person2 = {
  name: "mallang",
  age: 23,
  a() {
    console.log(this);
    console.log(this.name);
    let b = () => {
      console.log(this);
      console.log(this.name);
      let c = () => {
        console.log(this);
        console.log(this.name);
      };
      c();
	  // {name: 'mallang', age: 23, a: ฦ’}
	  // mallang
    };
    b();
	// {name: 'mallang', age: 23, a: ฦ’}
	// mallang
  },
};
person2.a();
// {name: 'mallang', age: 23, a: ฦ’}
// mallang
  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ this๋Š” ์ธ์Šคํ„ด์Šค
function Book(์ฑ…์ด๋ฆ„, ์ฑ…๊ฐ€๊ฒฉ, ์ €์ž, ์ถœํŒ์ผ) {
  this.์ถœ๋ ฅ = console.log(this);
  this.์ฑ…์ด๋ฆ„ = ์ฑ…์ด๋ฆ„;
  this.์ฑ…๊ฐ€๊ฒฉ = ์ฑ…๊ฐ€๊ฒฉ;
  this.์ €์ž = ์ €์ž;
  this.์ถœํŒ์ผ = ์ถœํŒ์ผ;
}

let newBook = new Book("Javascript", 1000, "์œค๋ง๋ž‘", "2023.04.13");

// Book {}
// ์ €์ž:  "์œค๋ง๋ž‘"
// ์ฑ…๊ฐ€๊ฒฉ: 1000
// ์ฑ…์ด๋ฆ„: "Javascript"
// ์ถœ๋ ฅ: undefined
// ์ถœํŒ์ผ: "2023.04.13"
  • ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ this๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ ํ•  ์ˆ˜ ์žˆ๋‹ค
[1, 2, 3].forEach(function () { console.log(this); }, [10, 20, 30]);
// [10, 20, 30]
// [10, 20, 30]
// [10, 20, 30]
let ์ธ์„ธ๊ทœ์ • = {
  ์ฑ…: 10,
  ์˜์ƒ์ฝ˜ํ…์ธ : 50,
};

function ์ธ์„ธ๊ณ„์‚ฐํ•จ์ˆ˜์ฑ…(e) {
  return e * (this.์ฑ… / 100);
}

function ์ธ์„ธ๊ณ„์‚ฐํ•จ์ˆ˜์˜์ƒ(e) {
  return e * (this.์˜์ƒ์ฝ˜ํ…์ธ  / 100);
}

console.log([100, 200, 300].map(์ธ์„ธ๊ณ„์‚ฐํ•จ์ˆ˜์ฑ…, ์ธ์„ธ๊ทœ์ •));	// [10, 20, 30]
console.log([100, 200, 300].map(์ธ์„ธ๊ณ„์‚ฐํ•จ์ˆ˜์˜์ƒ, ์ธ์„ธ๊ทœ์ •));	// [50, 100, 150]

ํ•จ์ˆ˜์˜ call(), apply(), bind() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž์˜ ์˜๋„๋Œ€๋กœ this๋ฅผ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜ ๊ณ ์ •ํ•ด ๋‘˜ ์ˆ˜ ์žˆ๋‹ค

  • call()
    ์ธ์ˆ˜์— this๋กœ ์‚ฌ์šฉํ•  ๊ฐ’๊ณผ ์•„๊ทœ๋จผํŠธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค
var peter = {
  name: "Peter Parker",
  sayName: function () {
    console.log(this.name);
  },
};

var bruce = {
  name: "Bruce Wayne",
};

peter.sayName.call(bruce);	// Bruce Wayne
peter.sayName(); 			// Peter Parker
var peter = {
  name: "Peter Parker",
  sayName: function (๊ฐํƒ„์‚ฌ) {
    console.log(this.name + ๊ฐํƒ„์‚ฌ);
  },
};

var bruce = {
  name: "Bruce Wayne",
};

peter.sayName.call(bruce, "!");	// Bruce Wayne!
peter.sayName("!");				// Peter Parker!
peter.sayName();				// Peter Parkerundefined
  • apply()
    ์ธ์ˆ˜์— this๋กœ ์‚ฌ์šฉํ•  ๊ฐ’๊ณผ ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ์•„๊ทœ๋จผํŠธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค
var peter = {
  name: "Peter Parker",
  sayName: function (is, is2) {
    console.log(this.name + " is " + is + " or " + is2);
  },
};

var bruce = {
  name: "Bruce Wayne",
};

peter.sayName.apply(bruce, ["batman", "richman"]);	// Bruce Wayne is batman or richman
  • bind()
    this๊ฐ€ ๊ณ ์ •๋œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
function sayName() {
  console.log(this.name);
}

var bruce = {
  name: "bruce",
  sayName: sayName,
};

var peter = {
  name: "peter",
  sayName: sayName.bind(bruce),
};

peter.sayName();	// bruce
bruce.sayName();	// bruce

๐Ÿช„ JSON

JSON.parse() : JSON ๋ฌธ์ž์—ด์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜

  • ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„์€ ๋ฐ˜๋“œ์‹œ ์Œ๋”ฐ์˜ดํ‘œ๋กœ ํ‘œํ˜„ํ•ด์•ผ ํ•œ๋‹ค
const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);
obj; // {result: true, count: 42}

const json = "[1, 2, 3]";
const arr = JSON.parse(json);
arr; // [1, 2, 3]

JSON.stringify() : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜

const json = { result: true, count: 42 };
const str = JSON.stringify(json);
str; // '{"result":true,"count":42}'
// ๊นŠ์€ ๋ณต์‚ฌ
let l = [10, 20, 30];
let a = JSON.parse(JSON.stringify(l));
a[0] = 1000;

console.log(l); // [10, 20, 30]

0๊ฐœ์˜ ๋Œ“๊ธ€