"๐Ÿ˜ญ ๋ฏธ์นœ ๋‚จ์นœ ํƒˆ์ถœ!"์ข…์†์„ฑ ์ฃผ์ž…์„ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค๋ช…

6638 ๋‹จ์–ด programmingbeginnersjavascript

๐Ÿ”ฅ ์œ„ํ—˜ํ•œ ์ƒํ™ฉ



์—ฌ์ž์—๊ฒŒ๋Š” ๋ฏธ์นœ ๋‚จ์ž ์นœ๊ตฌ๊ฐ€ ์žˆ์ง€๋งŒ ๊ทธ์—๊ฒŒ์„œ ๋ฒ—์–ด๋‚  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋…€๋Š” ๋‹ค๋ฅธ ์„ ํƒ์ด ์—†์Šต๋‹ˆ๋‹ค.

class Woman {
  makeLover(){
    const bf = new CrazyMan()
    bf.stayWith()
  }
}

class CrazyMan {
  stayWith() {
    console.log('I am dangerous man, stay with me')
  }
}

const woman = new Woman()
woman.makeLover()

// I am dangerous man, stay with me


๐Ÿ‘ฆ ์ข‹์€๋‚จ์ž๊ฐ€ ์žˆ๋‹ค!!



GoodMan ํด๋ž˜์Šค ์ƒ์„ฑ

class Woman {
  makeLover(){
    const bf = new CrazyMan()
    bf.stayWith()
  }
}

class CrazyMan {
  stayWith() {
    console.log('I am dangerous man, stay with me')
  }
}

// good man !!!!!!!
class GoodMan {
  stayWith() {
    console.log('I am good man, stay with me')
  }
}

const woman = new Woman()
woman.makeLover()

// ๐Ÿ˜ฐ But still she stays with dangerous man
// "I am dangerous man, stay with me"


์™œ ๊ทธ๋…€๋Š” ์—ฌ์ „ํžˆ ์œ„ํ—˜ํ•œ ๋‚จ์ž์™€ ํ•จ๊ป˜ ์žˆ์Šต๋‹ˆ๊นŒ ??
๊ทธ๋…€(์—ฌ์„ฑ๋ฐ˜)๋Š” ์œ„ํ—˜ํ•œ ๋‚จ์ž์—๊ฒŒ ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์—

class Woman {
  makeLover(){
    // here, crazy man exists in Woman class, he is aaaaaaalways with her
    const bf = new CrazyMan()
    bf.stayWith()
  }
}


๊ทธ๋ž˜์„œ ๊ทธ๋…€๋ฅผ ๋„์™€์ฃผ์„ธ์š” !! ์šฐ๋ฆฌ๋Š” ๋ฌด์—‡์„ํ•ด์•ผํ•ฉ๋‹ˆ๊นŒ ???

class Woman {
  // She does not depend on crazy man, she has choice now !!
  constructor(man) {
    this.man = man
  }
  makeLover(){
    this.man.stayWith()
  }
}

class CrazyMan {
  stayWith() {
    console.log('I am dangerous man, stay with me')
  }
}

class GoodMan {
  stayWith() {
    console.log('I am good man, stay with me')
  }
}

const man = new GoodMan()
const woman = new Woman(man)
woman.makeLover()

// I am good man, stay with me


์šฐ๋ฆฌ๋Š” ๋ฏธ์นœ ๋‚จ์ž์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ์ง€์›Œ์„œ ๊ทธ๋…€๋ฅผ ๋„์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜€


์ด๊ฒƒ์€ ์„ค๋ช…ํ•˜๊ธฐ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์ง€๊ธˆ๋ถ€ํ„ฐ ์˜์กด์„ฑ ์ฃผ์ž…์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ๊ธฐ์‚ฌ๋ฅผ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค๐Ÿ˜Ž

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ