Literals and constructors

Javascript Patterns(2011)

Javascript

2017-10-31


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋”ฉ ๊ธฐ๋ฒ•๊ณผ ํ•ต์‹ฌ ํŒจํ„ด(2011) ์ฑ…์„ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

๐Ÿ‘‰ 4์žฅ - Callback Pattern


1. ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ

// Bad
a = new Object()

// Good
a = {}
  • ์ด๋Ÿฐ์‹์œผ๋กœ Object, Array ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ๋ฆฌํ„ฐ๋Ÿด์„ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  • ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋นˆ ๊ฐ์ฒด๋กœ ๋ณด์ด์ง€๋งŒ ์‚ฌ์‹ค์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—๋Š” ๋นˆ ๊ฐ์ฒด๋Š” ์—†๋‹ค. ๋ชจ๋“  ๊ฐ์ฒด๋Š” ๋ถ€๋ชจ ๊ฐ์ฒด์ธ Object.prototype์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค.
a = {}
a.name = 'Benji'    // ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€
a.getName = function() {    // ๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€
    return a.name
}
a.name = 'Fido' // ํ”„๋กœํผํ‹ฐ ์ˆ˜์ •
delete a.name   // ํ”„๋กœํผํ‹ฐ ์‚ญ์ œ

2. ์‚ฌ์šฉ์ž ์ •์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜

  1. ๊ฐ์ฒด ์ƒ์„ฑ
let Person = function(name) {
    this.name = name
    this.say = function() {
        return "I am " + this.name
    }
}

let person = new Person('adam')
  1. Person์€ ์‚ฌ์‹ค ํ•จ์ˆ˜์ด์ž, ๊ฐ์ฒด์ด๋‹ค.
  2. new๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.

    • new๋Š” this ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋Š” ์ƒˆ๋กœ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.
    • ๋งŒ์•ฝ์— new ์—†์ด let person = Person('adam')๊ณผ ๊ฐ™์ด ๊ทธ๋ƒฅ ํ•จ์ˆ˜๋งŒ ํ˜ธ์ถœํ•œ๋‹ค๋ฉด ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ธฐ๋งŒ ํ•  ๋ฟ, this ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€ ๋ชปํ•œ๋‹ค. ๊ทธ๋ž˜์„œ Personํ•จ์ˆ˜์˜ this.name = name ์ด ๋ถ€๋ถ„์˜ this๋Š” ์ „์—ญ์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋˜๊ณ  name๊ณผ say()๋Š” ์ „์—ญ ๋ณ€์ˆ˜, ์ „์—ญ ๋ฉ”์†Œ๋“œ๋กœ ์„ธํŒ…์ด ๋œ๋‹ค. ๋งค์šฐ ์œ„ํ—˜ํ•œ ์ƒํ™ฉ์ด ๋˜๋Š” ์…ˆ์ด๋‹ค.
    • new๋ฅผ ์“ฐ๋ฉด ์œ„์˜ ์ฝ”๋“œ์˜ ์ด๋ฉด์— ์ง„ํ–‰๋˜๋Š” ๊ฒƒ์€ ์•„๋ž˜ ์ฃผ์„๊ณผ ๊ฐ™๋‹ค.
let Person = function(name) {
    // let this = {}
    this.name = name
    this.say = function() {
        return "I am " + this.name
    }
    // return this
}

let person = new Person('adam')
  • ํ•จ์ˆ˜ ๋‚ด์— return๋ฌธ์ด ์—†๋”๋ผ๋„ ์ƒ์„ฑ์ž๋Š” ์•”๋ฌต์ ์œผ๋กœ this๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. return๋ฌธ์„ ์จ์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•ด์ค„์ˆ˜๋„ ์žˆ๋‹ค.

์œ„์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค say() ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ณ„์† ๋งŒ๋“ค๊ฒŒ๋œ๋‹ค. ์ฆ‰, n๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด n๊ฐœ์˜ say()๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ๋ณด๋‹ค Person์˜ ํ”„๋กœํ† ํƒ€์ž…์— say() ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ํ•œ๋ฒˆ๋งŒ ์ •์˜ํ•˜๊ณ ๋„ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

Person.prototype.say = function() {
    return "I am " + this.name
}
  • new๋ฅผ ์“ฐ์ง€ ์•Š์•˜์„ ๋•Œ์—๋„ ์œ„์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ๊ฐ•์ œํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค. ์–ด๋–ค ๋ณ€์ˆ˜์— new์—†์ด Waffle()์„ ํ• ๋‹นํ•ด์„œ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰์ด ๋˜์—ˆ๋Š”๋ฐ ๊ทธ ์‹คํ–‰๋œ ํ•จ์ˆ˜์•ˆ์˜ this๊ฐ€ Waffle์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹ ๋•Œ, ์ฆ‰ ์ „์—ญ์„ ๊ฐ€๋ฆฌํ‚ฌ ๋•Œ ๊ทธ ํ•จ์ˆ˜ ์•ˆ์—์„œ new Waffle()๋กœ ์ƒˆ๋กœ์šด Waffle๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
function Waffle() {
    if (!(this isinstanceof Waffle)) {
        return new Waffle()
    }

    this.tastes = "yummy!"
}

Waffle.prototype.wantAnother = true

3. ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด

  1. ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๊ธฐ

๋ฐฐ์—ด๋„ ๊ฐ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‚ด์žฅ ์ƒ์„ฑ์ž Array()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋ณด๋‹ค let a = []์™€ ๊ฐ™์ด ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์•„๋ž˜ ๋‘๊ฐœ์˜ ์ฝ”๋“œ๋Š” ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

let a = [3]
// a.length = 1
// a[0] = 3

let a = new Array(3)
// a.length = 3
// typeof a[0] = "undefined"

์œ„์˜ ์ฝ”๋“œ๋Š” 3์ด๋ผ๋Š” ํ•˜๋‚˜์˜ ์ˆซ์ž๋ฅผ ๊ฐ€์ง„ ๋ฐฐ์—ด์ด ๋˜๊ณ , ์•„๋ž˜๋Š” 3๊ฐœ์˜ ์ž๋ฆฌ๋ฅผ ๊ฐ€์ง„ ๋นˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค.

  1. ๋ฐฐ์—ด ํŒ๋ณ„ํ•˜๊ธฐ

๋ฐฐ์—ด์€ isArray()๋กœ ํŒ๋ณ„ํ•œ๋‹ค. ๋ฐฐ์—ด๋„ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— typeof๋กœ ํŒ๋ณ„ํ•  ๊ฒฝ์šฐ, object๊ฐ€ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.


4. JSON

JSON(JavaScript Object Notation)์€ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์“ฐ์—ฌ์ง„ ๋ฐ์ดํ„ฐ ์ „์†ก ํ˜•์‹์ด๋‹ค.

  1. JSON.stringify(x) - JSON์€ ํ”„๋กœํผํ‹ฐ๋ช…์ด ํ•ญ์ƒ ๋ฌธ์ž์—ด์ด์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ stringify๋กœ ๋ชจ๋“  ํ‚ค๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ serializeํ•˜๋Š” ๊ฒŒ ์•ˆ์ „ํ•˜๋‹ค.
  2. JSON.parse(x) - JSON ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์—์„œ ๋˜์ ธ์ค„ ๋•Œ ์„œ๋ฒ„์—์„œ stringify๋ฅผ ํ•˜๊ณ  ํ”„๋ŸฐํŠธ์—์„œ ํŒŒ์‹ฑํ•ด์„œ ์“ฐ๋ฉด ์šฉ๋Ÿ‰์„ ์ ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.