const sum = new Function("a", "b", "console.log(a + b)");
sum(2, 4)
function sum (a, b) {
console.log(a + b)
}
sum(2, 4)
return
function abc() { // 기명함수
}
function () { // 익명함수, 선언 x, 콜백 함수 용도로 사용
}
const abc = function() {
}
const user = {
name: "hello",
age: 400
}
function getName({name, age, email = "all@naver.com"}) {
return [name, age, email]
}
console.log(getName(user))
arguments
객체;(익명함수)()
;(익명함수())
(function() {
console.log(33)
})()
const a = 7
double()
function double() {
console.log(a * 3)
}
function timeout(callback) {
setTimeout() => {
console.log('pansik')
callback()
}, 3000
}
timeout(() => {
console.log('apple!')
})
// timeout 함수를 호출할때 인수가 callback 매개변수로 들어간다.
function 대신 화살표 함수를 사용하여 간략한 방법으로 함수를 선언 할 수 있다.
() => {} vs function() {}
const double = function (x) {
return x * 2
}
console.log('double: ', double(7))
// 아래꺼랑 위에꺼는 똑같은 함수
const doubleArrow = (x) => {
return x * 2
}
console.log('doubleArrow', doubleArrow(7))
cosnt time = setInterval(function () {
console.log('pansik')
}, 3000) // 3초마다 pansik 출력이 된다.
const h1El = document.querySelector('h1')
h1El.addEventlistener('click', () => {
clearInterval(timer)
const timer = setTimeout(function () {
console.log('pansik')
}, 3000) // 3초뒤에 pansik
const h1El = document.querySelector('h1')
h1El.addEventlistener('click', () => {
clearTimeout(timer)
// h1을 클릭하면 타이머가 멈춘다.
})
const pansik = {
name: "Pansik",
normal: function () {
console.log(this.name)
},
arrow: () => {
console.log(this.name) // 함수 범위 안에서 정의가 된다.
}
}
pansik.normal() // Pansik
pansik.arrow() // 아무것도 안뜸
const timer = {
name: 'Pansik',
timeout: function () {
setTimeout(() => {
console.log(this.name)
}, 2000)
}
}
timer.timeout() // 2초뒤에 Pansik
class User {
constructor(first, last) {
this.firstName = first
this.lastName = last
}
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
const pansik = new User('Pansik' 'Kim')
const apple = new User('Apple' 'banana')
const neo = new User('Neo' 'Smith')
console.log(pansik)
console.log(apple.getFullName())
console.log(neo.getFullName())
if ()
else ()
else if ()
const inputEl = document.querySelector('input')
inputEl.addEventListener('keydown' , event => {
if (
event.key === 'Enter' ||
event.key === 'Escape'
) {
console.log('Wow!!')
}
})
const inputEl = document.querySelector('input')
inputEl.addEventListener('keydown' , event => {
switch (event.key) {
case 'Enter':
case 'Escape':
console.log('WWWW')
break
case 'Shift':
console.log('Hi~')
break
default:
}
})