JavaScript๐ŸŽˆ:: ํ•จ์ˆ˜, ๊ฐ์ฒด, ๋ฐฐ์—ด

Aprilยท2021๋…„ 4์›” 8์ผ
0

โœจJavascript

๋ชฉ๋ก ๋ณด๊ธฐ
2/45
post-thumbnail

๊ฐœ์ธ ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค

JavaScript

โ—Function ํ•จ์ˆ˜

  • Function ์ด๋ž€? ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ building block
  • subprogram์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๋ฉฐ ์—ฌ๋Ÿฌ๋ฒˆ ์žฌ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ
  • ํ•œ ๊ฐ€์ง€์˜ task ๋˜๋Š” ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

โ—‹Function declaration ์„ ์–ธ

๐ŸŽˆFunction์„ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

Function name (param1, param2..) {   // ํŒŒ๋ผ๋ฏธํ„ฐ
    body(ํ•จ์ˆ˜ ์•ˆ์— ๊ธฐ๋ณธ์ ์ธ business logic์„ ์ž‘์„ฑ)
    return;
}
  • one function === one thing ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋Š” ํ•œ ๊ฐ€์ง€์˜ ์ผ๋งŒ ํ•˜๋„๋ก ๋งŒ๋“ค๊ธฐ
  • Function์˜ ์ด๋ฆ„์„ ์ •์˜ํ•  ๋•Œ๋Š” ๋ฌด์—‡์„ ํ•  ๊ฒƒ์ธ๊ฐ€, ์–ด๋–ค๊ฒƒ์„ ๋ช…๋ นํ•  ๊ฒƒ์ธ๊ฐ€๋กœ ์ง€์ •
  • JS์—์„œ function์€ object ์ด๋‹ค
function printHello {   // ์ด๋ฆ„๋งŒ ์ •์˜ํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ฐ›์ง€ ์•Š์€ ์ƒํƒœ
    console.log('Hello');
}
printHello();

// ์กฐ๊ธˆ ๋” ์œ ์šฉํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด๋ฉด!
function log(message) {   // ์ด๋ฆ„ ์ •์˜ํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ›์€ ์ƒํƒœ
    console.log(message);
}
log('Hello');
log(1234);

ยบParameters

  • primitive parameters: ๋ฉ”๋ชจ๋ฆฌ์— value(๊ฐ’)์ด ๊ทธ๋Œ€๋กœ ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์ด ์ „๋‹ฌ๋จ
  • object parameters: ๋ฉ”๋ชจ๋ฆฌ์— reference๊ฐ€ ์ €์žฅ๋จ
function changename(obj) {
    obj.name = 'coder';
}
const april = { name: 'april'};   // april์ด๋ผ๋Š” ๋ฒˆ์ˆ˜์•ˆ์— april์ด๋ผ๋Š” object๋ฅผ ์ •์˜ํ•˜๊ณ  ํ• ๋‹น
changename(april);
console.log(april);   //coder

โœจ์ฐธ๊ณ โœจ Early Return, early exit

//bad case
function upgradeUser (user) {
    if (user.point > 10) {
    //long upgrade logic... ๋ธ”๋Ÿญ ์•ˆ์—์„œ ๋กœ์ง์„ ๋งŽ์ด ์ž‘์„ฑํ•˜๋ฉด ๊ฐ€๋…์„ฑ ์ €ํ•˜
    }
}

//good case
function upgradeUser (user) {
    if (user.point <= 10) {
    return; // ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋นจ๋ฆฌ returnํ•ด์„œ ํ•จ์ˆ˜ ์ข…๋ฃŒ
    }
    //long upgrade logic... ์กฐ๊ฑด์ด ๋งž์„ ๊ฒฝ์šฐ์— ์ž‘์„ฑํ•œ ๋กœ์ง ์‹คํ–‰
}

โ—‹Function expression ํ˜ธ์ถœ

ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ๋ณ€์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ• ๋‹น์ด ๋˜๊ณ , function์— parameter๋กœ ์ „๋‹ฌ์ด ๋˜๋ฉฐ return๊ฐ’์œผ๋กœ๋„ return์ด ๋œ๋‹ค

const print = function () {   // ํ•จ์ˆ˜์— ์ด๋ฆ„์ด ์—†๋Š” ๊ฒƒ์„ anonymous function
    // ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ๋ณ€์ˆ˜์— ํ• ๋‹น
    console.log('print');
}
print ();   // print๋ผ๋Š” ๋ณ€์ˆ˜์— ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ

const printAgain = print;   // ๋‹ค์‹œ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ํ• ๋‹น
printAgain();

const sumAgain = sum;
console.log(sumAgain(1, 3));

โ—‹Arrow Function

๐ŸŽ€ํ•จ์ˆ˜๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ํ•จ์ˆ˜

const simplePrint = funtion () {
    console.log('simplePrint!');
}

// โ†“ Arrow Function์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ
const simplePrint = () => console.log('simplePrint!');

// Arrow Function์˜ ๋˜ ๋‹ค๋ฅธ ์˜ˆ์ œ. ํ•œ ์ค„๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ
const add = (a, b) => a + b;

// Arrow Function ํ•œ ์ค„ ์ด์ƒ์ผ ๊ฒฝ์šฐ
const simpleMultiply = (a, b) => {
    //do something more
    retun a * b;
}

โ—Class ๊ฐ์ฒด์ง€ํ–ฅ์–ธ์–ด

๐Ÿšฉ์ง‘์ค‘! Class vs Object :: ํด๋ž˜์Šค์™€ ์˜ค๋ธŒ์ ํŠธ์˜ ์ฐจ์ด์ 

  • class๋Š” template๊ฐ™์€ ํ‹€,
  • ์ด ํ‹€์•ˆ์— data๋ฅผ ๋„ฃ์€ ๊ฒƒ์ด object
  • Class: ์—ฐ๊ด€์žˆ๋Š” data๋ฅผ ํ•œ ๊ณณ์— ๋ฌถ์–ด ๋†“์€ container
class person {
    name; // name, age ๊ฐ™์€ ์†์„ฑ (field)
    age;
    spaek(); // ํ–‰๋™ (method)
}
  • template: ์‹ค์ œ data๋Š” ๋“ค์–ด์žˆ์ง€ ์•Š๊ณ  ํ‹€๋งŒ ์„ ์–ธ *๋ถ•์–ด๋นตํ‹€

  • ํ•œ ๋ฒˆ๋งŒ ์„ ์–ธํ•˜๊ณ 

  • ๋ฐ์ดํ„ฐ๋Š” ๋“ค์–ด์žˆ์ง€ ์•Š๋‹ค

  • ๐ŸŽObject: class์˜ template์•ˆ์—data๋ฅผ ๋„ฃ์€ ๊ฒƒ

    ํŒฅ์„ ๋„ฃ์€ ๋ถ•์–ด๋นต, ํฌ๋ฆผ ๋„ฃ์€ ๋ถ•์–ด๋นต ๋“ฑ

    • class์˜ template์•ˆ์— instance๋ฅผ ์ƒ์„ฑํ•˜๋ฉด Object๊ฐ€ ๋œ๋‹ค
    • ์—ฌ๋Ÿฌ๋ฒˆ ์ƒ์„ฑ ๊ฐ€๋Šฅํ•˜๊ณ 
    • ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค

โ—‹Class declarations(Class ์„ ์–ธ)

class person {
    // constructor ์ƒ์„ฑ์ž
    constructor(name, age) {
        // fields
        this.name = name;
        this.age = age;
    }

    // methods
    spaek() {
        console.log('${this.name}: hello!');
    }
}

โ—‹Object ์ƒ์„ฑ

const april = new person ('april', 20); // ์ƒˆ๋กœ์šด object๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋Š” new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉ
console.log(april.name); // april
console.log(april.age); // 20
april.speak(); // april: hello!

โ—Object๐ŸŽ

  • Object ๋ž€? ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜• ์ค‘ ํ•˜๋‚˜
  • ์„œ๋กœ ์—ฐ๊ด€๋œ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ๊ทธ๋ฃนํ•‘ํ•˜๊ณ  ์ด๋ฆ„์„ ๋ถ™์ธ ๊ฒƒ
  • ๊ด€๋ จ ๋ฐ์ดํ„ฐ ๋˜๋Š” ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์ปฌ๋ ‰์…˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ธ์Šคํ„ด์Šค
  • object = { key : value } ; โ˜…โ˜…โ˜…โ˜… object๋Š” key์™€ value์˜ ์ง‘ํ•ฉ์ฒด์ด๋‹ค
// primetive ํƒ€์ž…์€ ๋ณ€์ˆ˜ ํ•˜๋‚˜๋‹น ํ•˜๋‚˜์˜ ๊ฐ’์„ ํ• ๋‹น
const name = 'april';
const age = 20;

print(name, age); // ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด name๊ณผ age๋ฅผ ๊ฐ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ
function print(name, age) { // ๋‘ ๊ฐ€์ง€์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉ
    console.log(name); // firstname, address๋“ฑ์˜ ์ธ์ž๊ฐ€ ๋งŽ์•„์ง€๊ฒŒ ๋˜๋ฉด ๊ด€๋ฆฌ/์šด์˜ํ•˜๊ธฐ ์–ด๋ ค์›€
}

// โ†“ Object๋กœ ๋ณ€๊ฒฝํ•ด๋ณด๋ฉด
function print(person) { 
    console.log(person.name);
}

const april = {nameL 'april', age: '20';} // ๊ฐ„ํŽธํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Œ
print(april);
// object๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ
const obj1 {}; // 'object literal' syntax(๋ฌธ๋ฒ•) {}๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“œ๋Š” ๊ฒƒ์„ object literal๋ผ๊ณ  ํ•จ
const obj2  = new Object(); // 'object constructor' syntax 
                   // new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋กœ class template๋ฅผ ์‚ฌ์šฉํ•ด์„œ object๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

delete april.age; // ์‚ญ์ œํ•˜๊ฑฐ๋‚˜
april.address = 'seoul'; // ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค

โ—Array() ๋ฐฐ์—ด(์ž๋ฃŒ๊ตฌ์กฐ)

โ—‹Array declaration(์„ ์–ธ)

  • ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ๋•Œ๋Š” new ๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์„ ์–ธ
  • []๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•ด์„œ ์„ ์–ธ
const arr1 = new array();
const arr2 = [1, 2];  // index 0๋ฒˆ์— '1'์ด๋ผ๋Š” data๊ฐ€ ๋“ค์–ด์žˆ๊ณ , index 1๋ฒˆ์— '2'๋ผ๋Š” data๊ฐ€ ์žˆ์Œ
const fruits = ['๐ŸŽ', '๐ŸŒ'];
console.log(fruits); // ๐ŸŽ, ๐ŸŒ
console.log(fruits.length); // 2
console.log(fruits[0]); // ๐ŸŽ
console.log(fruits[1]); // ๐ŸŒ
console.log(fruits[2]); // undefined
console.log(fruits[fruits.length - 1]); // ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ฐพ์„ ๋•Œ์—๋Š” ์ด ๊ธธ์ด(length)์—์„œ -1๋กœ ๊ณ„์‚ฐ

โ—‹๋ฐฐ์—ด ์ถœ๋ ฅ ๋ฐฉ๋ฒ•

1. for ๋ฌธ

const fruits = ['๐ŸŽ', '๐ŸŒ'];
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]); // ๐ŸŽ, ๐ŸŒ
}

2. for of ๋ฌธ

for (let fruit of fruits) {
    console.log(fruit); // ๐ŸŽ, ๐ŸŒ
}

โœ… ๋ชฉํ‘œ!

  • (์–ด๋ ค์›Œ์ง„๋‹ค;; ์ฐจ๊ทผ์ฐจ๊ทผ ํ•˜๋‚˜์”ฉ!!)
  • ์šฐ์„ ์€, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐœ๋…์„ ํŒŒ์•…ํ•˜์ž๐Ÿ˜‚

๋“œ๋ฆผ์ฝ”๋”ฉ by ์—˜๋ฆฌ
์ƒํ™œ์ฝ”๋”ฉ
MDN_Reference
MDN_Object

profile
๐Ÿš€ ๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ์“ฐ๋Š” ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ

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