koans - basic

마데슾 : My Dev Space·2019년 10월 21일
0

pass me 시험에 대비하여 기초를 탄탄하게 다지기 위해 koans 과제를 복습해 보았다.

Array 메소드 push, pop

  1. push
  • 기능 : 배열의 끝에 하나 이상의 요소를 추가
  • 리턴값 : 메서드를 호출한 배열의 새로운 length
  1. pop
  • 기능 : 배열의 마지막 요소 제거
  • 리턴값 : 제거된 요소
let array = [1, 2];
array.push(3); // 3
array // [1, 2, 3]

let poppedValue = array.pop();
poppedValue // 3
array // [1, 2]

Array 메소드 shift, unshift

  1. unshift
  • 기능 : 배열의 맨 앞쪽에 하나 이상의 요소를 추가
  • 리턴값 : 메서드를 호출한 배열의 새로운 length
  1. shift
  • 기능 : 배열의 첫번째 요소 제거
  • 리턴값 : 제거된 요소
let array = [1, 2];

array.unshift(3); // 3
array // [3, 1, 2]

let shiftedValue = array.shift();
shiftedValue // 3
array // [1, 2]

scope 함수

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!"

객체의 property로 담겨있는 function이 method처럼 작동하는지 확인

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" 

'this'에 관해서

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

객체에 property를 더하고 빼는법

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

prototype를 사용해 모든 instance 객체에 property와 method 추가하는 법

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"
profile
👩🏻‍💻 🚀

0개의 댓글