๐Ÿ“’ Javascript ํด๋ž˜์Šค :)

zooyahoยท2022๋…„ 3์›” 26์ผ
0

study with me

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

1. ์ƒ์„ฑ์ž ํ•จ์ˆ˜(prototype)

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ด๋ฆ„์€ ํŒŒ์Šค์นผ์ผ€์ด์Šค๋กœ ์ง€์Œ

function User(first, last) {
  // this = {}
  
  this.firstName = first
  this.lastName = last
  
  // return this
}

User.prototype.getFullName = function() {
	return `${this.firstName}` `${this.lastName}`
}

const amy = new User('amy', 'Clarke')
const zooyaho = new User('zooyaho', 'Park')

console.log(amy)
console.log(zooyaho.getFullName)

๐Ÿ‘‰ firstName๊ณผ lastName์€ user๋ผ๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅธ๋‚ด์šฉ์ด ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ†ต์ผํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ค์›€

๐Ÿ‘‰ getFullName์€ ๋กœ์ง์ด ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ํ†ต์ผํ™”ํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜์žˆ์Œ (ํ•จ์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์‚ฌ์šฉํ•จ)

โ— this

  • ์ผ๋ฐ˜(Nomal) ํ•จ์ˆ˜๋Š” "ํ˜ธ์ถœ ์œ„์น˜"์— ๋”ฐ๋ผ this์ •์˜
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ž์‹ ์ด ์„ ์–ธ๋œ "ํ•จ์ˆ˜ ๋ฒ”์œ„"์—์„œ this์ •์˜

๐Ÿ‘พ #1

const zooyaho = {
  name: 'Zooyaho',
  nomal: function() {
  	console.log(this.name)
  },
  arrow: ()=>{
  	console.log(this.name)
  }
}

zooyaho.nomal() // Zooyaho
zooyaho.arrow() // undefined

const amy = {
  name: 'Amy',
  nomal: zooyaho.nomal,
  arrow: zooyaho.arrow
}

amy.nomal() // amy
amy.arrow() // undefined

๐Ÿ‘‰ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ this๋Š” ํ™”์‚ดํ‘œํ•จ์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š” ํ•จ์ˆ˜ ์˜์—ญ์—์„œ ์ •์˜๊ฐ€ ๋จ

๐Ÿ‘พ #2

/* ์ผ๋ฐ˜ํ•จ์ˆ˜์—์„œ this */
const timer = {
  name: 'Zooyaho~!',
  timeout: function() {
  	setTimeout(function(){
      console.log(this.name)
    },2000)
  }
}

timer.timeout() // undefined

๐Ÿ‘‰ this๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜์—์„œ ๋งŒ๋“ค์–ด์ ธ ์žˆ๊ณ  setTimeoutํ•จ์ˆ˜ ๋กœ์ง ์–ด๋”˜๊ฐ€์—์„œ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— undefined๋ฅผ ๋ฐ˜ํ™˜ํ•จ

๐Ÿ‘พ #3

/* ํ™”์‚ดํ‘œํ•จ์ˆ˜์—์„œ this */
const timer = {
  name: 'Zooyaho~!',
  timeout: function() {
  	setTimeout(()=>{
      console.log(this.name)
    },2000)
  }
}

timer.timeout() // Zooyaho~!

๐Ÿ‘‰ ํ™”์‚ดํ‘œํ•จ์ˆ˜์˜ this๋Š” function()์˜ this์ด๊ณ  function()์˜ this๋Š” timer๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ ํ™”์‚ดํ‘œํ•จ์ˆ˜์˜ this.name์€ Zooyaho~!๋ฅผ ๊ฐ€๋ฆฌํ‚ด

๐Ÿ‘‰ setTimeout(), setInterval()๋ผ๋Š” ํƒ€์ด๋จธํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ฝœ๋ฐฑ์œผ๋กœ ์ผ๋ฐ˜ํ•จ์ˆ˜๋ณด๋‹ค๋Š” ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ํ™œ์šฉ๋„๊ฐ€ ๋†’์Œ

2. ES6 Classes

  • ํด๋ž˜์Šค๋Š” ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ผ๊ธ‰ ๊ฐ์ฒด์ด๋‹ค.
  • ํด๋ž˜์Šค๋Š” ํ•จ์ˆ˜๋กœ ํ‰๊ฐ€๋œ๋‹ค.
  • ํด๋ž˜์Šค ์„ ์–ธ๋ฌธ์œผ๋กœ ์ •์˜ํ•œ ํด๋ž˜์Šค๋Š” ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ๊ณผ ๊ฐ™์ด ์†Œ์Šค์ฝ”๋“œ ํ‰๊ฐ€ ๊ณผ์ •, ์ฆ‰ ๋Ÿฐํƒ€์ž„ ์ด์ „์— ๋จผ์ € ํ‰๊ฐ€๋˜์–ด ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด๋•Œ ํ•จ์ˆ˜ ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜, ์ฆ‰ constructor๋‹ค
  • ํ•จ์ˆ˜์ •์˜๊ฐ€ ํ‰๊ฐ€๋˜์–ด ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‹œ์ ์— ํ”„๋กœํ† ํƒ€์ž…๋„ ๋”๋ถˆ์–ด ์ƒ์„ฑ๋œ๋‹ค.
  • ํด๋ž˜์Šค๋Š” ์ •์˜ ์ด์ „์— ์ฐธ์กฐํ•  ์ˆ˜ ์—†๋‹ค.
  • ํด๋ž˜์Šค ํ‘œํ˜„์‹์œผ๋กœ ์ •์˜๋œ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ์‹๋ณ„์ž๋ฅผ ์‚ฌ์šฉํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•œ๋‹ค. ํด๋ž˜์Šค ์ด๋ฆ„์€ ํด๋ž˜์Šค ๋ชธ์ฒด ๋‚ด๋ถ€์—์„œ๋งŒ ์œ ํšจํ•œ ์‹๋ณ„์ž์ด๋‹ค!

1๏ธโƒฃ constructor() ๋ฉ”์„œ๋“œ

  • ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™” ํ•˜๊ธฐ ์œ„ํ•œ ํŠน์ˆ˜ํ•œ ๋ฉ”์„œ๋“œ์ด๋‹ค.
  • constructor ํ”„๋กœํผํ‹ฐ๋Š” ํด๋ž˜์Šค ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค.
  • constructor ๋‚ด๋ถ€์—์„œ this์— ์ถ”๊ฐ€ํ•œ ํ”„๋กœํผํ‹ฐ๋Š” ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋œ๋‹ค.
  • ๋ฉ”์„œ๋“œ๋กœ ํ•ด์„๋˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํด๋ž˜์Šค๊ฐ€ ํ‰๊ฐ€๋˜์–ด ์ƒ์„ฑํ•œ ํ•จ์ˆ˜ ๊ฐ์ฒด ์ฝ”๋“œ์˜ ์ผ๋ถ€๊ฐ€ ๋œ๋‹ค!!
  • constructor๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๋นˆ constructor๊ฐ€ ์•”๋ฌต์ ์œผ๋กœ ์ •์˜๋œ๋‹ค. ๋นˆ constructor๋Š” ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ๋ณ„๋„์˜ ๋ฐ˜ํ™˜๋ฌธ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ์•ˆ๋œ๋‹ค! new์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํด๋ž˜์Šค๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ this๋ฅผ ์•”๋ฌต์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค!

2๏ธโƒฃ ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ๋‹ค๋ฅด๊ฒŒ ํด๋ž˜์Šค์˜ prototypeํ”„๋กœํผํ‹ฐ์— ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ๊ฐ€ ๋œ๋‹ค!
class Person {
  // ์ƒ์„ฑ์ž
  constructor(name) {
    // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”
    this.name = name;
  }

  sayHi() {
    consoel.log(`Hi My name is ${this.name}`);
  }
}

const me = new Person("Lee");
me.sayHi(); // Hi My name is Lee

3๏ธโƒฃ ์ •์  ๋ฉ”์„œ๋“œ

  • ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•œ๋‹ค.
  • staticํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ๋œ๋‹ค!
  • ์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ์ธ์Šคํ„ฐ์Šค๋กœ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์† ๋ฐ›์„ ์ˆ˜ ์—†๋‹ค!

๐Ÿ‘พ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ •์  ๋ฉ”์„œ๋“œ ์ƒ์„ฑ

function Person(name) {
  this.name = name;
}
Person.sayHi = function () {
  console.log("Hi");
};
Person.sayHi(); // Hi

๐Ÿ‘พ class์—์„œ์˜ ์ •์  ๋ฉ”์„œ๋“œ ์ƒ์„ฑ

class Person(){
  constructor(name){
    this.name = name;
  }

  // ์ •์  ๋ฉ”์„œ๋“œ
   static sayHi(){
    consoel.log(`Hi`);
  }
}
Person.sayHi(); // Hi

๐Ÿ‘พ ์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค

class Person(){
  constructor(name){
    this.name = name;
  }

  // ์ •์  ๋ฉ”์„œ๋“œ
   static sayHi(){
    consoel.log(`Hi`);
  }
}
const me = new Person('Lee');
me.sayHi(); // TypeError

๐Ÿ‘พ #1

// ๊ฐ์ฒด ์•ˆ ๋ฉ”์†Œ๋“œ ์ถ•์•ฝํ˜•
const heropy = {
  name: 'zooyaho',
  normal() {
    console.log(this.name)
  }
}

๐Ÿ‘‰ normal(){} == normal: function(){}

๐Ÿ‘พ #2

// ์ƒ์„ฑ์žํ•จ์ˆ˜ ์ถ•์•ฝํ˜•
class User {
  constructor(first, last) {
    this.firstName = first
    this.lastName = last
  }
  getFullName() {
    return `${this.firstName} ${this.lastName}`
  }
}

๐Ÿ‘‰ getFullName()๋ฉ”์†Œ๋“œ๋ฅผ prototype์†์„ฑ์„ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  prototype์˜ ๋ฉ”์†Œ๋“œ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ!!!

4๏ธโƒฃ ์ƒ์†(ํ™•์žฅ)

class Vehicle {
  constructor(name, wheel) {
    this.name = name
    this.wheel = wheel
  }
}

const myBicycle = new Vehicle('๋‘๋ฐœ ์ž์ „๊ฑฐ', 2)
console.log(myBicycle)

class Car extends Vehicle {
  constructor(name, wheel, license) {
    super(name, wheel)
    this.license = license
  }
}

const myCar = new Car('๋ฒค์ธ ', 4, true)
console.log(myCar)

์ฐธ๊ณ  ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive

profile
์ฆ๊ฒ๊ฒŒ ๊ฐœ๋ฐœํ•˜์ž ์ฅฌ์•ผํ˜ธ๐Ÿ‘ป

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