Skip to content

Latest commit

ย 

History

History
317 lines (231 loc) ยท 7.26 KB

File metadata and controls

317 lines (231 loc) ยท 7.26 KB

JavaScript ์œ ์šฉํ•œ ๋ฌธ๋ฒ• ๐ŸŒผ

์ด๋ฒˆ์—” Javascript์˜ ์œ ์šฉํ•œ ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

์‚ผํ•ญ์—ฐ์‚ฐ์ž

? ์™€ : ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด๋ฌธ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ค‘์ฒฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋ณด๋‹จ, if ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ด๋‹ค.

const hansome = true;

const value = hansome == true ? "๋‚˜๋Š” ์ž˜์ƒ๊ฒผ๋‹ค~" : "๋‚˜๋Š” ๋ชป์ƒ๊ฒผ๋‹ค~";
console.log(value);

์ด๋Ÿฐ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด hansome ์˜ ๊ฐ’์ด true ์ด๋ฏ€๋กœ ๋‚˜๋Š” ์ž˜์ƒ๊ฒผ๋‹ค~ ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.


Truthy & Falsy

Falsy์˜ ๊ฐ’์—๋Š” undefined , null , 0 , ๋น„์–ด์žˆ๋Š” ๋ฌธ์ž์—ด ,NaN ์ด ์žˆ๋‹ค.


๋‹จ์ถ• ํ‰๊ฐ€ ๋…ผ๋ฆฌ ๊ณ„์‚ฐ๋ฒ•

// &&
console.log(true && 'true๊ฐ’์ด ์•„๋‹ˆ๋ฏ€๋กœ ์ด ๋ฌธ์žฅ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.');

// ||
console.log(false || 'false๊ฐ’์ด๋ฏ€๋กœ ์ด ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.');

์ด๋Ÿฐ ์‹์œผ๋กœ ๋‹จ์ถ• ํ‰๊ฐ€ ๋…ผ๋ฆฌ ๊ณ„์‚ฐ๋ฒ•์—์„œ && ์€ ์•ž์˜ ๊ฒƒ์ด true ๋ฉด true ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ˆ๋ฉด ๋’ค์˜ ๊ฒƒ์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ || ์€ ์•ž์˜ ๊ฒƒ์ด false ๋ฉด ๋’ค์˜ ๊ฒƒ์„ ์ถœ๋ ฅํ•œ๋‹ค.

ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ

const MyHeight = (height) => {
    return height;
}

const result = MyHeight(180);
console.log(result);

๋งŒ์•ฝ ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑ์„ ํ•˜๋ฉด 180 ์ด๋ผ๋Š” ๊ฐ’์ด ์ถœ๋ ฅ ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์•ˆ ์จ์ฃผ๊ณ  ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์ •์„ ํ•ด์ฃผ๋ ค๋ฉด ์–ด๋–ค ์‹์œผ๋กœ ํ•ด์•ผํ• ๊นŒ?

const MyHeight = (height = 180) => {
    return height;
}

const result = MyHeight();
console.log(result);

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•จ์ˆ˜์•ˆ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฏธ๋ฆฌ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.


์กฐ๊ฑด๋ฌธ ๋” ์Šค๋งˆํŠธํ•˜๊ฒŒ ์“ฐ๊ธฐ

function AnimalSound(animal) {
        if(animal === 'dog') return '์™ˆ์™ˆ';
        if(animal === 'elephant') return '๋ฟŒ์šฐ์šฐ';
        if(animal === 'croco') return '์•„๊ฑฐ์•„๊ฑฐ';
        if(animal === 'rino') return '์ฝ”๋ฟ”์ฝ”๋ฟ”';

        return '์†Œ๋ฆฌ๊ฐ€ ์•ˆ๋“ค๋ ค!';
}

console.log(AnimalSound('dog'));
console.log(AnimalSound('elephant'));
console.log(AnimalSound('croco'));
console.log(AnimalSound('rino'));
console.log(AnimalSound('Human'));

์ด๋Ÿฐ์‹์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋Š” ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ€๋…์„ฑ ์žˆ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

function AnimalSound(animal) {
    const tasks= {
        dog() {
            console.log('์™ˆ์™ˆ!');
        },
        elephant() {
            console.log('๋ฟŒ์šฐ์šฐ!');
        },
        croco() {
            console.log('์•„๊ฑฐ์•„๊ฑฐ');
        },
        rino() {
            console.log('์ฝ”๋ฟ”์ฝ”๋ฟ”');
        }
    };
    if(!tasks[animal]) {
    console.log('์†Œ๋ฆฌ๊ฐ€ ์•ˆ๋“ค๋ ค!');
    return;
    }
    
    tasks[animal]();
}

AnimalSound('dog');
AnimalSound('elephant');
AnimalSound('croco');
AnimalSound('rino');
AnimalSound('Human');

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์€ ๊ฐ์ฒด์™€ ๋ฐฐ์—ด,ํ•จ์ˆ˜๋“ฑ์—์„œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด์—์„œ์˜ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น

const Object = {name: 'Taehwan', age: 18};

const {name, age} = Object;

console.log(name);
console.log(age);

๋ฐฐ์—ด์—์„œ์˜ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น

const Array = ['Taehwan', 18];

const [name, age] = Array;

console.log(name);
console.log(age);

ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ์˜ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์€ ์ด์ „์˜ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์šธ๋•Œ ์ž ๊น ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค.

๊ฐ’์ด ์ฃผ์–ด์กŒ์„๋•Œ

const Introduce = {
    height: 180,
    weight: 200
};

function print({height, weight}) {
    console.log(height);
    console.log(weight);
}

print(Introduce);

๊ฐ’์ด ์•ˆ์ฃผ์–ด์กŒ์„๋•Œ

const Introduce = { height: 180 };

function print({height, weight = 200}) {
    console.log(height);
    console.log(weight);
}

print(Introduce);

spread์™€ rest

spread ๋Š” ๊ฐ์ฒดํ˜น์€ ๋ฐฐ์—ด์—์„œ ๊ฐ™์€ ์ธ์ž๋ฅผ ํผํŠธ๋ฆด์ˆ˜ ์žˆ๋‹ค.

spread

๊ฐ์ฒด

const Goblin = {
    name: 'goblin'
};

const Powergoblin = {
    ...Goblin,
    power: true
};

const Kinggoblin = {
    ...Powergoblin,
    army: true
};

console.log(Goblin);
console.log(Powergoblin);
console.log(Kinggoblin);

๋ฐฐ์—ด

const Goblin = ['goblin'];
const Powergoblin = [...Goblin, 'Power!'];
const Kinggoblin = [...Goblin, ...Powergoblin, 'King has army!'];

console.log(Goblin);
console.log(Powergoblin);
console.log(Kinggoblin);

์ด๋Ÿฐ์‹์œผ๋กœ ... ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

rest

rest ๋Š” ๊ฐ์ฒด,๋ฐฐ์—ด,ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด

const Kinggoblin = {
    name: 'goblin',
    power: true,
    army: true
};

const {name, ...rest} = Kinggoblin;

console.log(name);
console.log(rest);

๋ฐฐ์—ด

const Kinggoblin = ['Goblin', 'Im strong!', 'The king has army!'];

const [name, ...rest] = Kinggoblin;

console.log(name);
console.log(rest);

์ด๋Ÿฐ ์‹์œผ๋กœ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฐฐ์—ด์—์„œ์˜ rest ๋Š” ๋งจ ์•ž์— ๋‚˜์˜ฌ ์ˆ˜ ์—†๋‹ค.

ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ์˜ rest

function add(...rest) {
    return rest.reduce((acc, current) => acc + current, 0);
}

console.log(add(1,2,3,4,5,6,7));

์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด rest ๊ฐ€ add ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋“ค์–ด๊ฐ„๋‹ค. ๊ทธ ์ดํ›„ ๋ฐ›์•„์˜ค๋Š” ๊ฐ’๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ›์•„์˜ค๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ๋ฐฐ์—ด ๋‚ด์žฅํ•จ์ˆ˜์ธ reduce ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›๋Š” ๊ฐ’๋“ค์„ ๋‹ค ๋”ํ•ด์ค€๋‹ค. ๊ฒฐ๊ณผ๋Š” 28 ์ด ๋‚˜์˜จ๋‹ค.

ํ•จ์ˆ˜ ์ธ์ž์—์„œ์˜ spread

  • ํŒŒ๋ผ๋ฏธํ„ฐ: ํ•จ์ˆ˜ ์•ˆ์—์„œ ๊ด„ํ˜ธ์•ˆ์— ๋ฐ›์•„์˜ค๋Š” ๊ฐ’
  • ์ธ์ž : ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜์•ˆ์— ๋„ฃ์–ด์ฃผ๋Š” ๊ฐ’
function add(...rest) {
    return rest.reduce((acc, current) => acc + current, 0);
}
const spread = [1,2,3,4,5,6,7];
console.log(add(...spread));

์ด๋Ÿฐ์‹์œผ๋กœ ์ธ์ž ์•ˆ์—์„œ spread๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.


scope

  • scope ๋ž€ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ์–ด๋””์„œ ์–ด๋””๊นŒ์ง€ ์œ ํšจํ•œ์ง€์˜ ๋ฒ”์œ„๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • Global , Function , Block ๊ฐ€ ์žˆ๋‹ค.
  • Global : ์ฝ”๋“œ ์ „์—ญ์—์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
  • Function : ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
  • Block : ์กฐ๊ฑด๋ฌธ ๋“ฑ์˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
const result = 'Im Global!';

function Say() {
    const result = 'Im Function!';

    if (true) {
        const result = 'Im Block!';
        console.log(result);
    }
    console.log(result);
}

Say();
console.log(result);
  • ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋งจ ์ฒ˜์Œ์˜ result ๋Š” Global ์ด๋ฏ€๋กœ ์ฝ”๋“œ ์–ด๋А๊ณณ์—์„œ๋“  ํ˜ธ์ถœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‘๋ฒˆ์งธ result ๋Š” function ์ด๋ฏ€๋กœ ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • if ๋ฌธ ์•ˆ์˜ result ๋Š” Block ์ด๋ฏ€๋กœ if ๋ฌธ ์•ˆ์—์„œ๋งŒ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

hoisting

hoisting ์ด๋ž€ ์„ ์–ธ์ „์— ํ˜ธ์ถœ์ด ๋˜์—ˆ์„๋•Œ ์„ ์–ธ์„ ์ฝ”๋“œ ๋งจ ์œ„๋กœ ์˜ฌ๋ ค์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. let ๊ณผ const ๋Š” hoisting ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

Say();

function Say() {
    console.log('Hello๐Ÿ‘‹');
}

์ด๋Ÿฐ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋„ ์‹คํ–‰์ด ๋œ๋‹ค.


์ถœ์ฒ˜ : ํŒจ์ŠคํŠธ์บ ํผ์Šค ๊ฐ•์˜ - ๋ฒจ๋กœํผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์œ ์šฉํ•œ ๋ฌธ๋ฒ•