function calcAge1(birthYear){
return 2037 - birthYear;
}
const age1 = calcAge1(1991);
console.log(age1);
const calcAge2 = function(birthYear){
return 2037 - birthYear;
} // expression으로 값을 생성.
const age2 = calcAge2(1991);
console.log(age2)
// function declaration
const age1 = calcAge1(1991);
function calcAge1(birthYear){
return 2037 - birthYear;
}
console.log(age1);
// age1 출력 가능함.
// function expression
const age2 = calcAge2(1991);
const calcAge2 = function(birthYear){
return 2037 - birthYear;
}
console.log(age2);
// 에러 발생.
const calcAge3 = birthYear => 2037 - birthYear;
// 간단한 한 줄의 함수에 대해서
// 중괄호가 필요 없고
// 반환이 암묵적으로 일어난다.
const age3 = calcAge3(1991);
console.log(age3);
const yearsUntilRetirement = birthYear => {
const age = 2037 - birthYear;
const retirement = 65 - age;
return retirement;
}
// 코드가 두 줄 이상인 경우 return이 필요하다.
console.log(yearsUntilRetirement(1991));
const yearsUntilRetirement = (birthYear, firstName) => {
const age = 2037 - birthYear;
const retirement = 65 - age;
return `${firstName} retires in ${retirement} years`;
}
// 매개변수가 두 개 이상인 경우 괄호가 필요하다.
console.log(yearsUntilRetirement(1991,
'Jonas'));
function cutFruitPieces(fruit){
return fruit*4;
}
function fruitProceessor(apples, oranges){
const applePieces = cutFruitPieces(apples);
const orangePieces = cutFruitPieces(oranges);
const juice = `Juice with ${applePieces} apples and ${orangePieces} oranges.`;
return juice;
}
console.log(fruitProcessor(2,3));
// 리터럴 구문, 대괄호 사용
const friends = ['Michael', 'Steven', 'Peter'];
// 키워드, Array 함 사용
const years = new Array(1991, 1984, 2008, 2020);
const friends = ['Michael', 'Steven', 'Peter'];
friends[2] = 'Jay'; // 변경됨. ['Michael', 'Steven', 'Jay']
// 주의: primitive value가 불변이라는 것이지, 배열은 primitive하지 않는다.
// 메모리 저장 방식과 관련이 있다. 추후 설명 예정.
friends=['Bob', 'Alice']; // ❌. 에러 발생함.
friends[4] = 'Eugene'; // 4번 인덱스에 값이 추가된다.
// 배열
const jonasArray = [
'Jonas',
'Schmedtmann',
2037-1991
'teacher',
['Michael', 'Peter', 'Steven']
];
// 객체
const jonas = {
firstName: 'jonas',
lastName: 'Schmedtmann',
age: 2037-1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven']
}; // 5개의 속성을 가진 jonas 객체
.
을 사용한다.console.log(jonas.lastName); // Schmedtmann
[]
을 사용한다.console.log(jonas['lastName']); // Schmedtmann
const nameKey = 'Name';
console.log(jonas['first' + nameKey]);
console.log(jonas['first' + nameKey]);
const interestedIn = prompt("관심있는 것은? firstName, lastName, age, job, friends 중에 하나 작성");
if(jonas[interestedIn]){
console.log(jonas[interestedIn]);
} else {
console.log('Wrong request! Choose between firstName, lastName, age, job, and friends');
보통은 dot notation을 사용하고, 위의 응용 방법이 필요할 때(속성 이름을 계산해야 할 때) bracket notation 사용한다.
const jonas = {
firstName: 'jonas',
lastName: 'Schmedtmann',
age: 2037-1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven']
};
jonas.location = 'Portugal';
jonas['twitter'] = '@jonasschmedtman';
console.log(jonas); // location과 twitter 속성이 추가되어 있다.
const jonas = {
firstName: 'jonas',
lastName: 'Schmedtmann',
age: 2037-1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
hasDriversLicense: true,
calcAge: function(birthYear){
return 2037 - birthYear
},
};
console.log(jonas.calcAge(1991)); // 46
console.log(jonas['calcAge'](1991)); // 46
const jonas = {
firstName: 'jonas',
lastName: 'Schmedtmann',
birthYear: 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
hasDriversLicense: true,
// calcAge: function(birthYear){
// return 2037 - birthYear
// },
calcAge: function(){
return 2037 - this.birthYear; // jonas.birthYear
}
};
console.log(jonas.calcAge()); // 46
const jonas = {
firstName: 'jonas',
lastName: 'Schmedtmann',
birthYear: 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
hasDriversLicense: true,
// calcAge: function(birthYear){
// return 2037 - birthYear
// },
calcAge: function(){
this.age = 2037 - this.birthYear;
}
};
jonas.calcAge();
console.log(jonas.age); // 46
const jonas = {
firstName: 'jonas',
lastName: 'Schmedtmann',
birthYear: 1991,
job: 'teacher',
friends: ['Michael', 'Peter', 'Steven'],
hasDriversLicense: true,
calcAge: function(){
this.age = 2037 - this.birthYear;
return this.age
},
getSummary: function(){
if (this.hasDriversLicense) console.log(`${this.firstName} is a ${this.calcAge()}-
year old ${this.job}, and he has ${this.hasDriversLicense? 'a' : 'no'} driver's license`);
}
};
jonas.getSummary(); // jonas is a 46-year old teacher, and he has a driver's license
const jonas = [
'Jonas',
'Schmedtmann',
2037-1991,
'teacher',
['Michael','Peter','Steven'],
true
];
const types = [];
for (let i=0 ; i<jonas.length ; i++){
// Reading from jonas array
console.log(jonas[i], typeof jonas[i]);
// Filling type array
types[i] = typeof jonas[i];
// 또는 push 메서드를 사용할 수도 있다.
// types.push(typeof jonas[i]);
}
console.log(types);
// typeof 응용하기
for (let i=0 ; i< jonas.length ; i++){
if (typeof jonas[i] !== 'string') continue;
console.log(jonas[i], typeof jonas[i]);
}
for (let i= jonas.length-1 ; i>=0 ; i--){
...
}