한빛미디어의 <러닝리액트>를 공부하며 작성하는 글입니다.
// 함수 선언
function logCompliment() {
console.log("하이");
}
// 선언한 함수 호출
logCompliment();
// 출력 : 하이
const logCompliment = function() {
console.log("하이");
}
// 선언한 함수 호출
logCompliment();
// 출력 : 하이
// 선언하기 전에 함수 호출
hey();
// 함수 선언
function hey() {
alert("hey");
}
// 잘 작동함
// 선언하기 전에 함수 호출
hey();
// 함수 표현식
const hey = function(){
alert("hey");
}
// error : hey is not a function
const logCompliment = function(firstName) {
console.log(`잘했어요, ${firstName}`);
}
logCompliment("말감");
// 출력 : 잘했어요, 말감
const logCompliment = function(firstName) {
return(`잘했어요, ${firstName}`);
}
console.log(logCompliment("말감"));
// 출력 : 잘했어요, 말감
// logCompliment에서 return 받은 값을 콘솔로그에 출력
function logActivity(name="말감", activity="테니스") {
console.log(`${name}은 ${activity}를 좋아해.`);
}
logActivity();
// 출력 : 말감은 테니스를 좋아해.
const defaultPerson = {
name : {
first : "말감",
last : "이"
},
favActivity : "테니스"
}
function logActivity(p = defaultPerson) {
console.log(`${p.name.first}은 ${p.favActivity}를 좋아해.`);
}
logActivity();
// 출력 : 말감은 테니스를 좋아해.
const lordify = function(firstname) {
return `베리베리의 ${firstname}라고?!`;
}
console.log(lordify('연호'));
// 베리베리의 연호라고?!
const lordify = (firstname) => `베리베리의 ${firstname}라고?!`;
console.log(lordify('연호'));
// 베리베리의 연호라고?!
const person = (firstName, lastName) =>
({
first : firstName,
last : lastName
});
console.log(person("말감", "이"));
const gangwon = {
resort : ["용평", "평창", "강릉"],
print : function(delay = 1000) { // 여기서 this는 gangwon
setTimeout(function() { // 하지만 여기서 this는 window
console.log(this.resorts.join(","));
}, delay)
}
}
gangwon.print();
// error : Cannot read property 'join' of undefined
console.log(this); // window{}
const gangwon = {
resort : ["용평", "평창", "강릉"],
print : function(delay = 1000) {
setTimeout(() => {
console.log(this.resorts.join(","));
}, delay)
}
}
gangwon.print(); //"용평", "평창", "강릉"