pass me 시험에 대비하여 기초를 탄탄하게 다지기 위해 koans 과제를 복습해 보았다.
let array = [1, 2];
array.push(3); // 3
array // [1, 2, 3]
let poppedValue = array.pop();
poppedValue // 3
array // [1, 2]
let array = [1, 2];
array.unshift(3); // 3
array // [3, 1, 2]
let shiftedValue = array.shift();
shiftedValue // 3
array // [1, 2]
let message = "Outer";
function getMessage() {
return message;
}
function overrideMessage() {
let message = "Inner"; // 지역 변수
// let 키워드를 이용해 새로 선언,
// 전역에 선언한 message와 다른 변수이다
return message;
}
getMessage() // "Outer"
overrideMessage() // "Inner"
message // "Outer"
function makeIncreaseByFunction(increaseByAmount) {
return function (numberToIncrease) {
return numberToIncrease + increaseByAmount;
};
}
const increaseBy3 = makeIncreaseByFunction(3);
const increaseBy5 = makeIncreaseByFunction(5);
increaseBy3(10) + increaseBy5(10) // 28
const appendRules = function (name) {
return name + " rules!";
};
const appendDoubleRules = function (name) {
return name + " totally rules!";
};
const praiseSinger = { givePraise: appendRules };
praiseSinger.givePraise("John") // "John rules!"
praiseSinger.givePraise = appendDoubleRules;
praiseSinger.givePraise("Mary") // "Mary totally rules!"
const meglomaniac = {
mastermind : "Brain",
henchman: "Pinky",
battleCry: function (noOfBrains) {
return "They are " + this.henchman + " and the" +
Array(noOfBrains + 1).join(" " + this.mastermind);
}
};
const battleCry = meglomaniac.battleCry(4);
battleCry // "They are Pinky and the Brain Brain Brain Brain"
const currentDate = new Date()
const currentYear = (currentDate.getFullYear());
const meglomaniac = {
mastermind: "James Wood",
henchman: "Adam West",
birthYear: 1995,
calculateAge: function () {
return currentYear - this.birthYear;
}
};
currentYear; // 2019
meglomaniac.calculateAge() // 24
const meglomaniac = { mastermind : "Agent Smith", henchman: "Agent Smith" };
"secretary" in meglomaniac; // false
meglomaniac.secretary = "Agent Smith";
"secretary" in meglomaniac; // true
delete meglomaniac.henchman;
"henchman" in meglomaniac // false
function Circle(radius) {
this.radius = radius;
}
let simpleCircle = new Circle(10);
let colouredCircle = new Circle(5);
colouredCircle.colour = "red"; // "red"
simpleCircle.colour; // undefined
colouredCircle.colour; // "red"
Circle.prototype.describe = function () {
return "This circle has a radius of: " + this.radius;
};
simpleCircle.describe(); // "This circle has a radius of: 10"
colouredCircle.describe(); // "This circle has a radius of: 5"