
선언과 표현 그리고 호이스팅
function hello() {}
const hello = function() {}
hello()
function hello(){
console.log('Hello');
}
hello()
const hello = function(){
console.log('hello');
}
반환 및 종료
function plus(num){
if(typeof num !== 'number'){
console.log('숫자를 입력해주세요!');
return 0
}
return num + 1
}
console.log(plus(7));
console.log(plus(2));
console.log(plus());
매개변수 패턴
function sum(a, b = 1){
return a + b
}
console.log(sum(1, 2));
console.log(sum( 7));
const { get } = require("lodash")
const user = {
name : 'chan',
age : 27,
}
function getName({name}){
return name
}
function getEmail({email = '이메일이 없습니다.'}){
return email
}
console.log(getName(user));
console.log(getEmail(user));
const fruits = ['Apple', 'Banana', 'Cherry']
const numbers = [1,2,3,4,5,6,7]
function getSecondItem([,b]){
return b
}
console.log(getSecondItem(fruits));
console.log(getSecondItem(numbers));
function sum(...rest){
return rest.reduce(function(acc, cur){
return acc + cur
}, 0)
}
console.log(sum(1,2,));
console.log(sum(1,2,3,4,5));
console.log(sum(1,2,3,4,5,6,7,8,9,10));
화살표 함수
const sum = (a, b) => a + b
console.log(sum(1,2));
console.log(sum(10,20));
const a = () => {}
const b = x => {}
const c = (x, y) => {}
const d = x => x * x
const e = x => x * x
const f = x => {
console.log(x * x);
return x * x
}
const g = () => { return { a : 1}}
const h = () => {{ a : 1}}
const i = () => { return [1,2,3]}
const j = () => [1,2,3]
즉시실행함수(IIFE)
const a = 7
const double = () => {
console.log(a * 2);
}
double();
(() => {
console.log(a * 2);
})()
;(() => {})()
;(function () {})()
;(function () {}())()
;!function () {}()
;+function () {}()
;((a,b) => {
console.log(a.innerWidth);
console.log(b.body);
})(window, document)
콜백
const a = callback => {
callback()
console.log('A');
}
const b = () => {
console.log('B');
}
a(b)
const sum = (a,b,c) => {
setTimeout(() => {
c(a+b)
}, 1000)
}
sum(1,2,value => {
console.log(value)
})
sum(3,5,value => {
console.log(value)
})
const loadImage = (url, cb) => {
const imgEl = document.createElement('img')
imgEl.src = url
imgEl.addEventListener('load', () => {
setTimeout(() => {
cb(imgEl)
},1000)
})
}
const containerEl = document.querySelector('.container')
loadImage('https://www.gstatic.com/webp/gallery/4.jpg',(imgEl)=> {
containerEl.innerHTML= ''
containerEl.append(imgEl)
})
재귀
let i = 0
const a = () =>{
console.log('A');
i++
if(i<4){
a()
}
}
a()
const userA = {
name:'A',
parent:null
}
const userB = {
name:'B',
parent:userA
}
const userC = {
name:'C',
parent:userB
}
const userD = {
name:'D',
parent:userC
}
const getRootUser = user => {
if(user.parent) {
return getRootUser(user.parent)
}
return user
}
console.log(getRootUser(userD));
호출 스케줄링
const hello = () => {
console.log('Hello~');
}
const timeout = setTimeout(hello, 2000)
const h1El = document.querySelector('h1')
h1El.addEventListener('click', () => {
clearTimeout(timeout)
})
const hello = () => {
console.log('Hello~');
}
const timeout = setInterval(hello, 2000)
const h1El = document.querySelector('h1')
h1El.addEventListener('click', () => {
console.log('Clear');
clearInterval(timeout)
})
this
const user = {
firstName : 'baek',
lastName : 'seungchan',
age: 27,
getFullName: function(){
return `${this.firstName} ${this.lastName}`
}
}
console.log(user.getFullName());
function user(){
this.firstName = 'hello'
this.lastName = 'bye~~'
return{
firstName : 'baek',
lastName : 'seungchan',
age: 27,
getFullName: () => {
return `${this.firstName} ${this.lastName}`
}
}
}
const u = user()
console.log(u.getFullName());
function user(){
this.firstName = 'hello'
this.lastName = 'bye~~'
return{
firstName : 'baek',
lastName : 'seungchan',
age: 27,
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
}
const lewis = {
firstName: 'easy',
lastName: 'hard'
}
const u = user()
console.log(u.getFullName());
console.log(u.getFullName.call(lewis));
const timer = {
title: 'TIMER!',
tiemout(){
console.log(this.title);
setTimeout(()=>{
console.log(this.title);
},2000)
}
}
timer.tiemout()
많은 것을 배웠습니다, 감사합니다.