iPod Video White

πŸ’» dev/πŸ‘©πŸ»‍πŸ’» ν•€ν…Œν¬ ν”„λ‘ νŠΈμ—”λ“œ 개발자 κ³Όμ •

[220419] JS function

k_m_jin 2022. 4. 19. 22:20

πŸ“’ μ‹€κ°•

ν•¨μˆ˜ (functions)

new Function()

μƒμ„±μž ν•¨μˆ˜ 호좜둜 ν•¨μˆ˜λ‘œ λ§Œλ“œλŠ” 법!
근데 자주 μ•ˆ 씀.

 const sum = new Function('a','b','console.log(a+b)') //클래슀(객체)
//μ„œλ²„μ—μ„œ κ°€μ Έμ˜¬λ• json으둜 밖에 μ•ˆλŒ
sum(2,4)

λ°˜ν™˜κ³Ό μ’…λ£Œ

return
undefined

return ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λ°˜ν™˜ν•˜κ³  ν•¨μˆ˜ μ’…λ£Œ!

return 이 μ—†λ‹€λ©΄ undefined λ‚˜μ˜΄

function sum (a ,b) {
  return console.log(a + b) //retuen 이 μ—†μœΌλ©΄ undefined κ°€ λ‚˜μ˜΄
  console.log(123) //λ‚˜μ˜€μ§€ μ•ŠμŒ 
}

μ„ μ–Έκ³Ό ν‘œν˜„

λ§€κ°œλ³€μˆ˜ νŒ¨ν„΄

  • κΈ°λ³Έκ°’ μ„€μ •
    function sum (a= 1 , b = 3 ) { return a+b } console.log(sum(4)) // 기본값이 μ—†λ‹€λ©΄ NaN
  • ꡬ쑰 λΆ„ν•΄ ν• λ‹Ή (Destructuring assignment)
    const user = { name: 'Heropy', age: 85 } function getName({name, age}){ //name: email return [name ,age] } console.log(getName(user))
  • λ‚˜λ¨Έμ§€ λ§€κ°œλ³€μˆ˜ (Rest parameter)
    - ...rest λŠ” λ°°μ—΄λ°μ΄ν„° 
  • function sum(...rest){ 
    return rest.reduce(function (acc, cur){ return acc + cur }) 
    // return rest.reduce((acc, cur) => acc + cur) } 
    console.log(1, 2, 3, 4) //10
  • aguments 객체 : λͺ¨λ“  인수λ₯Ό λ°›μ•„λ“€μ΄λŠ” μ•”μ‹œμ μΈ λ³€μˆ˜

IIFE

Immediately-Invoked Function Expression
μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜ (ν‘œν˜„)
읡λͺ…ν•¨μˆ˜μ— 주둜 μ‚¬μš©
;(읡λͺ…ν•¨μˆ˜)()
;(읡λͺ…ν•¨μˆ˜()) ꢌμž₯

console.log(456) //;κ°€ μ—†λ‹€λ©΄ error

;(function (){
  console.log(123)
})()

ν˜Έμ΄μŠ€νŒ… Hosting

ν•¨μˆ˜ μ„ μ–Έμ—μ„œλ§Œ λ°œμƒ
ν‰κ°€λ λ•Œ μ„ μ–ΈλΆ€λ₯Ό μœ„λ‘œ λŒμ–΄μ˜¬λ¦¬λŠ” ν˜„μƒ

콜백

콜백 ν•¨μˆ˜ (callback function)
ν•¨μˆ˜μ˜ 인수둜 μ‚¬μš©λ˜λŠ” ν•¨μˆ˜

function abc(callback){
  callback()
}

abc(function(){
  console.log('ABC')
})
setTimeout(abc(), 1000)
// setTimeout(undefined, 1000)

//setTimeout μ „μ—­ ν•¨μˆ˜
setTimeout(abc, 1000)

setTimeout(function(){
  console.log('ABC')
}, 1000)
function sum(a, b, cb){
  setTimeout(function (){
    cb (a + b)
  }, 1000)
}

sum(2, 5, function(res){
  console.log(res)
})

220421

ν™”μ‚΄ν‘œ ν•¨μˆ˜

const hello = () => console.log('hello'); //좜λ ₯μ•ˆλŒ
const hello = message => `hello ${messages}`
function hello(){
   return 'hello~'
 }

 const hello = () => {
   return 'Hello~'
}
  1. 리턴 λ¬Έ ν•œμ€„μ΄λ©΄ μ€‘κ΄„ν˜Έ μƒλž΅κ°€λŠ₯
  2. λ§€κ°œλ³€μˆ˜κ°€ ν•˜λ‚˜μΌλŒ€λ§Œ μ†Œκ΄„ν˜Έ μƒλž΅κ°€λŠ₯
  3. 객체 데이터λ₯Ό 리턴없이 λ°˜ν™˜ν• λ• μ†Œκ΄„ν˜Έλ‘œ 감싼닀.

this

  • 일반 ν•¨μˆ˜μ™€ ν™”μ‚΄ν‘œ ν•¨μˆ˜μ˜ 차이점
  • μ „μ—­ 객체
  1. 일반 ν•¨μˆ˜μ—μ„œμ˜ This
    ν˜ΈμΆœλ˜λŠ” μœ„μΉ˜μ—μ„œ μ •μ˜
const heropy = {
  name: 'heropy',
  age: 85,
  phone: '01012345678',
  isVaile: true,
  getName: function() {
    return [this.name, this.age]
  }
}

console.log(heropy.getName())
const heropy = {
  name: 'heropy',
  age: 85,
  phone: '01012345678',
  isVaile: true,
  getName: function() {
    return [this.name, this.age]
  }
}
const amy = {
  name: 'Amy',
  age: 22
}
console.log(heropy.getName.call(amy))
  1. ν™”μ‚΄ν‘œ ν•¨μˆ˜
    μ„ μ–Έλœ μœ„μΉ˜μ—μ„œ μ •μ˜
    ν™”μ‚Ίν‘œ ν•¨μˆ˜λ₯Ό 감싸고 μžˆλŠ” ν•¨μˆ˜κ°€ thisκ°€ 됌(lecical scope)
const heropy = {
  name: 'heropy',
  age: 85,
  phone: '01012345678',
  isVaile: true,
  getName: () => {
    return [this.name, this.age]
  }
}
const amy = {
  name: 'Amy',
  age: 22
}
console.log(heropy.getName.call(amy))
function kmjin () {
  this.name = 'wrapper'
  this.age = 99
  const heropy = {
    name: 'heropy',
    age: 85,
    phone: '01012345678',
    isVaile: true,
    getName: () => {
      return [this.name, this.age]
    }
  }
  const amy = {
    name: 'Amy',
    age: 22
  }
  console.log(heropy.getName.call(amy))
}
kmjin()
const userA = {
  isVaild: true,
  age: 85,
  getAge: function () {
    return this.age
  }
}
const userB = {
  isVaild: false,
  age: 22,
  getAge: function () {
    return this.age
  }
}

console.log(userB.getAge.apply(userA))
//call apply 호좜용 
// λ‹€λ₯Έ 객체의 κΈ°λŠ₯을 λΉŒλ¦΄λ–„
//bind μ—°κ²°μš©
const userA = {
  isVaild: true,
  age: 85,
  getAge: function () {
    setTimeout(function(){
      console.log(this.age)
    })
  }
}

userA.getAge() //undefined
const userA = {
  isVaild: true,
  age: 85,
  getAge: function () {
    setTimeout(() => {
      console.log(this.age)
    })
  }
}

userA.getAge() //85
λ°˜μ‘ν˜•