๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

200229

ยท ์•ฝ 6๋ถ„

JavaScript#

๊ฐ์ฒด#

  • ๊ฐ์ฒด ์„ ์–ธ
const ironMan = {    'name': 'ํ† ๋‹ˆ์Šคํƒ€ํฌ',    'actor': '๋กœ๋ฒ„ํŠธ ๋‹ค์šฐ๋‹ˆ ์ฃผ๋‹ˆ์–ด',    'alias': '์•„์ด์–ธ๋งจ',    'key is string': true,};

๊ฐ์ฒด๋Š” { } ๋กœ ๊ฐ์‹ธ๊ณ  key: value ํŽ˜์–ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๋ฐ, key ๋Š” ๋ฌธ์ž์—ด๋กœ ๊ณต๋ฐฑ์ด ์žˆ๋‹ค๋ฉด ' ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค. ๋˜ํ•œ , ๋ฅผ ๋นผ๋†“์œผ๋ฉด error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  • ๊ฐ์ฒด ํ˜ธ์ถœ
console.log(ironMan);// Object {name: "ํ† ๋‹ˆ์Šคํƒ€ํฌ", actor: "๋กœ๋ฒ„ํŠธ ๋‹ค์šฐ๋‹ˆ ์ฃผ๋‹ˆ์–ด", alias: "์•„์ด์–ธ๋งจ"}
console.log(ironMan.name);// ํ† ๋‹ˆ์Šคํƒ€ํฌ
console.log(ironMan.actor);// ๋กœ๋ฒ„ํŠธ ๋‹ค์šฐ๋‹ˆ ์ฃผ๋‹ˆ์–ด
console.log(ironMan.alias);// ์•„์ด์–ธ๋งจ
  • ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด
const {alias, name, actor} = hero;const text = `${alias}(${name}) ์—ญํ• ์„ ๋งก์€ ๋ฐฐ์šฐ๋Š” ${actor} ์ž…๋‹ˆ๋‹ค.`;console.log(text);

๊ฐ์ฒด์—์„œ ๊ฐ’์„ ์ถ”์ถœํ•ด๋ƒ„.

  • ๊ฐ์ฒด ๋‚ด๋ถ€ ํ•จ์ˆ˜
const dog = {    name: '๋ฉ๋ฉ์ด',    sound: '๋ฉ๋ฉ!',    say: function () {        console.log(this.sound);    },};

function() ์€ ์ต๋ช… ํ•จ์ˆ˜์ด์ง€๋งŒ, function dogFunc() ์ฒ˜๋Ÿผ named ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด๋„ ๋œ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๊ฐ์ฒด ๋‚ด๋ถ€์— ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

const dog = {    name: '๋ฉ๋ฉ์ด',    sound: '๋ฉ๋ฉ!',    say: () => {        console.log(this.sound);    },};
dog.say();
// TypeError: Cannot read property 'sound' of undefined

function ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ this ๋กœ ์ž์‹ ์ด ์†ํ•œ ์œ„์น˜์™€ ์—ฐ๊ฒฐ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅ.

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ์ž๊ธฐ ์ž์‹ ์ด ์†ํ•œ this ๊ฐ€ ์–ด๋””์ธ์ง€ ๋ชจ๋ฅธ๋‹ค

const dog = {    name: '๋ฉ๋ฉ์ด',    sound: '๋ฉ๋ฉ!',    say: function () {        console.log(this.sound);    },};
const cat = {    name: '์•ผ์˜น์ด',    sound: '์•ผ์˜น~',};
cat.say = dog.say;dog.say(); // ๋ฉ๋ฉ!cat.say(); // ์•ผ์˜น~
// Errorconst dogSay = dog.say;const catSay = cat.say;dogSay();catSay();

cat.say = dog.say; ๋กœ ํ•จ์ˆ˜๋ฅผ ํ• ๋‹นํ•ด์ค„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋งŒ์•ฝ ์™ธ๋ถ€์— ์žˆ๋Š” const dogSay ์— ํ• ๋‹นํ•œ๋‹ค๋ฉด, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ this ๊ฐ€ ์–ด๋Š scope ์ธ์ง€๋ฅผ ์•Œ์ง€ ๋ชปํ•ด error ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  • getter์™€ setter ํ•จ์ˆ˜
// EX 1const dog = {    _name: '๋ฉ๋ฉ์ด',    get name() {        console.log('_name์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.');        return this._name;    },    set name(value) {        console.log('์ด๋ฆ„์ด ๋ฐ”๋€๋‹ˆ๋‹ค..' + value);        this._name = value;    },};
console.log(dog.name);dog.name = '๋ญ‰๋ญ‰์ด';console.log(dog.name);
// EX 2const numbers = {    _a: 1,    _b: 2,    sum: 3,    calculate() {        console.log('calculate');        this.sum = this._a + this._b;    },    get a() {        return this._a;    },    get b() {        return this._b;    },    set a(value) {        this._a = value;        this.calculate();    },    set b(value) {        this._b = value;        this.calculate();    },};
console.log(numbers.sum);numbers.a = 5;numbers.a = 6;numbers.a = 7;numbers.a = 8;console.log(numbers.sum);

get ํ•จ์ˆ˜์™€ sum ํ•จ์ˆ˜๋Š” ์ด๋ฆ„์ด ๊ฐ™์•„๋„ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๋ณ€์ˆ˜์™€ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์•ˆ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ณ€์ˆ˜๋ช… ์•ž์— _ ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.

EX2์—์„œ์ฒ˜๋Ÿผ get, set์„ ์‚ฌ์šฉํ•  ๋•Œ์˜ ์žฅ์ ์€ a์˜ ๊ฐ’์ด ๋ณ€ํ•  ๋•Œ์— calculate ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ sum ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๋ฏ€๋กœ ํšจ์œจ์ ์ด๋‹ค.

  • ๊ฐ์ฒด ๋‚ด์žฅ ํ•จ์ˆ˜
const doggy = {    ์ด๋ฆ„: '๋ฉ๋ฉ์ด',    ์†Œ๋ฆฌ: '๋ฉ๋ฉ',};
console.log(doggy);
console.log(Object.entries(doggy));console.log(Object.keys(doggy));console.log(Object.values(doggy));

๋ฐฐ์—ด#

JS์—์„œ ๋ฐฐ์—ด์€ ํƒ€์ž…์„ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ex) [ 'a', 1, 'text' ]

๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜

  • push

  • pop

  • length

๋ฐ˜๋ณต๋ฌธ#

  • for ๋ฌธ

  • while ๋ฌธ

  • for - of

const numbers = [10, 20, 30, 40, 50];
for (let number of numbers) {    console.log(number);}

์ปฌ๋ ‰์…˜ ์ „์šฉ ๋ฐ˜๋ณต ๊ตฌ๋ฌธ

  • for - in
var obj = {    a: 1,    b: 2,    c: 3,};
for (var prop in obj) {    console.log(prop, obj[prop]); // a 1, b 2, c 3}

๊ฐ์ฒด์˜ ์†์„ฑ๋“ค์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ž‘์—… ์ˆ˜ํ–‰.

  • continue

๋ฐ˜๋ณต๋ฌธ ์ค‘์— ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์‹คํ–‰.

  • break

๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ.

  • foreach
const superheroes = ['์•„์ด์–ธ๋งจ', '์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด', 'ํ† ๋ฅด', '๋‹ฅํ„ฐ ์ŠคํŠธ๋ ˆ์ธ์ง€'];
superheroes.forEach((hero) => {    console.log(hero);});
  • map
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const square = (n) => n * n;const squared = array.map(square);
console.log(squared);
// ๋” ๊ฐ„๋žตํžˆconst array = [1, 2, 3, 4, 5, 6, 7, 8];const squared = array.map(n => n * n;);
console.log(squared);
  • indexOf
const superheroes = ['์•„์ด์–ธ๋งจ', '์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด', 'ํ† ๋ฅด', '๋‹ฅํ„ฐ ์ŠคํŠธ๋ ˆ์ธ์ง€'];const index = superheroes.indexOf('ํ† ๋ฅด');console.log(index);
  • findIndex
const todos = [    {        id: 1,        text: '์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž…๋ฌธ',        done: true,    },    {        id: 2,        text: 'ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',        done: true,    },    {        id: 3,        text: '๊ฐ์ฒด ๋ฐฐ์—ด ๋ฐฐ์šฐ๊ธฐ',        done: true,    },    {        id: 4,        text: '๋‚ด์žฅ ํ•จ์ˆ˜ ๋ฐฐ์šฐ๊ธฐ',        done: false,    },];
const index = todos.findIndex((todo) => todo.id === 3);console.log(index);
  • find

  • filter