lil.dev
Published on

๐ŸŽ‰ JS(3)-๋“œ๋ฆผ์ฝ”๋”ฉ ๊ฐ•์˜ ์ •๋ฆฌ

๊ธ€์“ด์ด

    ๐Ÿ“Œ ๋ชฉ์ฐจ

    Welcome

    โœจ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ ๊ฐ•์˜ (ES5+): ๊ฐ™์ด ๋…ธํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋ฉฐ ๋ฐฐ์›Œ์š” ๐Ÿ“’

    ๐Ÿ’๐Ÿป

    1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์—ญ์‚ฌ์™€ ๋ชฉ์  ๋ฐ ๋™ํ–ฅ
    2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋™์ž‘์›๋ฆฌ

    ๋ฐ์ดํ„ฐํƒ€์ž…(3๊ฐ•)

    ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ œ์ผ ์ค‘์š”ํ•œ ๊ฒƒ : ์ž…๋ ฅ.์—ฐ์‚ฐ.์ถœ๋ ฅ(์ „์†ก)

    ๋ณ€์ˆ˜(Variable), rw(read/write):๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ ํ›„์—๋„ ์ฝ๊ณ  ์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅ

    ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋Š” ๊ฐ’.

    ์ฝ”๋“œ๋ธ”๋Ÿญ
    ์ฝ”๋“œ ๋ฐ–์—์„œ๋Š” ์ฝ”๋“œ๋ธ”๋Ÿญ ์•ˆ์— ์žˆ๋Š” ์ฝ”๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.
    let (added in ES6)
    ํ•œ๊ฐ€์ง€ ๋ฐ•์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ. ์—ฌ๊ธฐ์— ๋‹ค๋ฅธ ๊ฐ’์„ ๋„ฃ์–ด ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค.
    let name = 'apopo'
    console.log(name)
    name = 'hello'
    console.log(name)
    
    var(don't ever use this!)
    ES6์ด์ „์— ์“ฐ๋˜ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ช…๋ น์–ด

    ๊ฐ’์„ ํ• ๋‹นํ•˜๊ธฐ๋„ ์ „์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ (undefined) ์ด์ œ๋Š” ์“ฐ์ง€ ์•Š๋Š”๋‹ค.
    ์–ด๋””์— ์„ ์–ธํ–ˆ๋Š”์ง€ ์ƒ๊ด€์—†์ด ๊ฐ’์„ ๋Œ์–ด์˜ฌ๋ ค์ฃผ๋Š” ๊ฑธ hoisting์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ
    var์€ hoisting์„ ์‚ฌ์šฉํ•ด์„œ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ๊ฐ€์žฅ ์œ„๋กœ ๋Œ์–ด์˜ฌ๋ฆฐ๋‹ค.
    ๋˜ํ•œ var์€ ์ฝ”๋“œ๋ธ”๋Ÿญ๋„ ๋ฌด์‹œํ•˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ๋œ๋‹ค.

    ๊ณ ์ •๊ฐ’ (Constants) r(read):๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋œ ํ›„์—๋Š” ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅ

    ๋˜๋„๋ก์ด๋ฉด const๋ฅผ ์“ฐ๊ณ  ์„ ์–ธ ํ›„ ๋‚˜์ค‘์— ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•  ๋•Œ๋งŒ let์„ ์“ฐ์ž.

    const (added in ES6)
    ํ•œ๋ฒˆ ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด ๋”์ด์ƒ ๊ฐ’์„ ๊ต์ฒดํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด. ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์ž ๊ฒจ์žˆ์Œ

    immutable datatype์ด๋‹ค. ์ด๋ ‡๊ฒŒ ๋ชป๋ฐ”๊พธ๊ฒŒ ํ•˜๋Š” ์ด์œ ๋Š” 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

    • security
    • thread safety
    • reduce human mistakes

    ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค (Variable types)

    • primitive, Single item (๋”์ด์ƒ ์ž‘๊ฒŒ ๋‚˜๋ˆŒ ์ˆ˜ ์—†๋Š” ํ•œ๊ฐ€์ง€์˜ ๋ฐ์ดํ„ฐํƒ€์ž…):
      number, string, boolean, null, undefined, symbol
    • object, box container : ์‹ฑ๊ธ€ ํƒ€์ž…๋“ค์„ ๋ฌถ์–ด์„œ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๊ด€๋ฆฌ
    • function, first-class function(๋ณ€์ˆ˜์— ํ• ๋‹น ๊ฐ€๋Šฅ, ์ธ์ž๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ, ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

    ๋กœ์šฐ ๋ ˆ๋ฒจ ์–ธ์–ด(c,java)์˜ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค๋„ ๊ณต๋ถ€ํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์‚ฌ์ด์ฆˆ๋ฅผ ๋”์šฑ ์„ธ์„ธํ•˜๊ฒŒ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. Js์—์„œ๋Š” number๋งŒ ์“ฐ๋ฉด ์–ด๋–ค ํฌ๊ธฐ์˜ ์ˆ˜๋“  ์“ธ ์ˆ˜ ์žˆ๋‹ค! ์•„์ฃผ ๊ฐ„๋‹จ!

    ํŠน๋ณ„ํ•œ ์ƒํ™ฉ์—์„œ์˜ number ๊ฐ’๋“ค

    const infinity = 1 / 0 //Infinity๋กœ ์ถœ๋ ฅ๋จ
    const negativeInfinity = -1 / 0 //negativeInfinity์œผ๋กœ ์ถœ๋ ฅ๋จ
    const nAn = 'not a number' / 2 //NaN์œผ๋กœ ์ถœ๋ ฅ๋จ
    const bigInt = 12345678975654897486465135487694861531553n //bigint๋ผ๊ณ  ์ถœ๋ ฅ๋จ
    

    string

    const char = 'c' //ํ•œ๊ฐ€์ง€์˜ ๊ธ€์ž๋“  ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ธ€์ž๋“  ๋‹ค string์œผ๋กœ ํ• ๋‹น
    const brendan = 'brendan'
    const greeting = 'hello' + brendan
    console.log(`value: ${greeting}, type: ${typeof greeting}`)
    const helloBob = `hi ${brendan}!` //template literals (string) :
        ``(๋ฐฑํ‹ฑ)๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด ์›ํ•˜๋Š” string์„ ${}์•ˆ์— ์ ์œผ๋ฉด ๊ทธ string์˜ ๊ฐ’์ด ์ถœ๋ ฅ๋จ
    console.log(`value: ${helloBob}, type: ${typeof helloBob}`)
    

    boolean

    • false: 0, null, undefined, NaN, ''
    • true: any other value
    const canRead = true
    const test = 3 < 1 //false
    console.log(`value" ${canRead}, type: ${typeof canRead}`)
    console.log(`value: ${test}, type: ${typeof test}`)
    

    null
    null์ด๋ผ๊ณ  ํ• ๋‹นํ•˜๋ฉด ์•„์˜ˆ ๋นˆ ๊ฐ’์„ ์ง€์ •ํ•ด์ค€๊ฒƒ

    let nothing = null
    console.log(`value: ${nothing}, type: ${typeof nothing}`)
    

    undefined
    ์•„๋ฌด ๊ฐ’์ด ์ง€์ •๋˜์–ด ์žˆ์ง€ ์•Š์€ ์ƒํƒœ

    let x
    console.log(`value: ${x}, type: ${typeof x}`)
    

    symbol
    create unique identifiers for objects
    ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ,๊ณ ์œ ํ•œ ์‹๋ณ„์ž๋ฅผ ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์”€
    ๋™์ผํ•œ ๋‚ด์šฉ์„ ๊ฐ€์ง€๊ณ  ์‹ฌ๋ณผ์„ ๋‹ค๋ฅด๊ฒŒ ๋งŒ๋“ค๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ์‹ฌ๋ณผ๋กœ ๊ฐ„์ฃผํ•จ.

    const symbol1 = Symbol('id')
    const symbol2 = Symbol('id')
    console.log(symbol1 === symbol2) //false
    

    ๋งŒ์•ฝ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋‹ค๋ฅธ ์‹ฌ๋ณผ์„ ๋งŒ๋“ค์—ˆ์„ ๋•Œ ๋™์ผํ•œ ์‹ฌ๋ณผ๋กœ
    ๊ฐ„์ฃผํ•˜๊ณ  ์‹ถ์œผ๋ฉด Symbol.for();์„ ์‚ฌ์šฉํ•œ๋‹ค.

    const gsymbol1 = Symbol.for('id')
    const gsymbol2 = Symbol.for('id')
    console.log(gsymbol1 === gsymbol2) //true
    

    object

    const apopo = { name: 'apopo', age: 20 }
    //ํ•œ๋ฒˆ ํ• ๋‹น๋œ object๋Š” ๋‹ค๋ฅธ object๋กœ ํ• ๋‹น์ด ๋ถˆ๊ฐ€ํ•˜์ง€๋งŒ
    //object์•ˆ์—์„œ๋Š” ๋‹ค๋ฅธ ๊ฐ’๋“ค์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.
    //apopo.name,apopo.age๋“ฑ์œผ๋กœ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
    

    ๋‹ค์ด๋‚˜๋ฏน ํƒ€์ดํ•‘(dynamically typed language)

    ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ 
    ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๋™์ž‘ํ•  ๋•Œ ํ• ๋‹น๋œ ๊ฐ’์— ๋”ฐ๋ผ ํƒ€์ž…์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค.(์ž๋™ํ˜•๋ณ€ํ™˜)
    ์ด ์ ์ด ๋‹จ์ ์œผ๋กœ ์ž‘์šฉํ•  ๋•Œ๊ฐ€ ์žˆ์–ด TypeScript๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ๋‹ค.

    let text = 'hello'
    console.log(`value: ${text}, type: ${typeof text}`) //hello, string
    text = 1
    console.log(`value: ${text}, type: ${typeof text}`) //1, number
    text = '7' + 5
    console.log(`value: ${text}, type: ${typeof text}`) //75, string
    text = '8' / '2'
    console.log(`value: ${text}, type: ${typeof text}`) //2, number
    
    Note!
    Immutable data types: premitive types, frozen objects (i.e. object.freeze()) Mutable data
    types: all objects by default are mutable in JS favor immutable data type always for a few reasons:
        - security
        - thread safety
        - reduce human mistakes