π’ μ€κ°
ν¨μ (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~'
}
- λ¦¬ν΄ λ¬Έ νμ€μ΄λ©΄ μ€κ΄νΈ μλ΅κ°λ₯
- λ§€κ°λ³μκ° νλμΌλλ§ μκ΄νΈ μλ΅κ°λ₯
- κ°μ²΄ λ°μ΄ν°λ₯Ό 리ν΄μμ΄ λ°νν λ μκ΄νΈλ‘ κ°μΌλ€.
this
- μΌλ° ν¨μμ νμ΄ν ν¨μμ μ°¨μ΄μ
- μ μ κ°μ²΄
- μΌλ° ν¨μμμμ 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))
- νμ΄ν ν¨μ
μ μΈλ μμΉμμ μ μ
νμΊν ν¨μλ₯Ό κ°μΈκ³ μλ ν¨μκ° 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
λ°μν
'π» dev > π©π»βπ» νν ν¬ νλ‘ νΈμλ κ°λ°μ κ³Όμ ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Project] css ν΄λ‘ μ½λ© (0) | 2022.04.29 |
---|---|
[JS] νμ€ λ΄μ₯ κ°μ²΄ (μ λ°μ΄νΈ μμ ) (0) | 2022.04.25 |
[220413] npm / packae.json /parcel (0) | 2022.04.13 |
[Node.js] nvm μ€μΉ (0) | 2022.04.13 |
[220405] css selector (0) | 2022.04.06 |