[TIL] 210917

Lee SyongΒ·2021λ…„ 9μ›” 17일
0

TIL

λͺ©λ‘ 보기
30/204
post-thumbnail

πŸ“ 였늘 ν•œ 것

  1. array.slice() / μ˜ˆμ•½μ–΄ arguments / 길이λ₯Ό κ°–λŠ” 빈 λ°°μ—΄ / array.join() / Date() / ν‚€μ›Œλ“œ in / 객체 μƒμ„±μž ν•¨μˆ˜ & new μ—°μ‚°μž / private variable

  2. javascript-koans 풀이


πŸ“š 배운 것

javascript-koans

ν’€λ©΄μ„œ λͺ¨λ₯΄κ±°λ‚˜ ν—·κ°ˆλ Έλ˜ λΆ€λΆ„ 정리


1. AboutArrays.js

1) array.slice(begin, end);

var array = ["peanut", "butter", "and", "jelly"];

console.log(array.slice(2, 2));
console.log(array.slice(2, 1));
// [] β†’ begin이 end와 κ°™κ±°λ‚˜ end보닀 큰 κ²½μš°μ—λŠ” '빈 λ°°μ—΄'을 좜λ ₯ν•œλ‹€

console.log(array.slice(5, 1));
// [] β†’ begin이 λ°°μ—΄μ˜ 길이보닀 큰 κ²½μš°μ—λŠ” '빈 λ°°μ—΄'을 좜λ ₯ν•œλ‹€

console.log(array.slice(3, 0));
// [] β†’ begin이 λ°°μ—΄μ˜ 길이와 같은 κ²½μš°μ—λŠ” '빈 λ°°μ—΄'을 좜λ ₯ν•œλ‹€

2. AboutFunctions.js

1) μ˜ˆμ•½μ–΄ arguments

function returnAllArgs() {
  var argsArray = [];
  for (var i = 0; i < arguments.length; i += 1) {
    argsArray.push(arguments[i]);
  }
  console.log(argsArray);
  // ['first', 'second', 'third']
}

returnAllArgs('first', 'second', 'third');

3. AboutObjects.js

1) 길이λ₯Ό κ°–λŠ” 빈 λ°°μ—΄(Array) 생성

var array = new Array(5);

console.log(array);
// [undefined, undefined, undefined, undefined, undefined]

2) array.join();

(1) κ°œλ…

var array = new Array(5);

console.log(array.join(''));
// ''

console.log(array.join(' '));
// '    '

console.log(arrya.join(' ' + 'coke'));
// ' coke coke coke coke'

(2) μ‘μš©

var megalomaniac = {
  mastermind : "Brain",
  henchman: "Pinky",
  battleCry: function (noOfBrains) {
    return "They are " + this.henchman + " and the" +
      Array(noOfBrains + 1).join(" " + this.mastermind);
  }
};

console.log(megalomaniac.battleCry(4));
// 'They are Pinky and the Brain Brain Brain Brain'

3) Date()

ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ„ μ½μ–΄μ˜€κ±°λ‚˜ μ„€μ •ν•  수 μžˆλ‹€

(1) Date 객체 생성

var date = new Date(); // ν˜„μž¬ λ‚ μ§œμ™€ μ‹œκ°„μ„ μ½μ–΄μ˜΄
var date = new Date(λ…„, μ›”, 일); 
var date = new Date(λ…„, μ›”, 일, μ‹œ, λΆ„, 초);

(2) Date 객체의 λ‚ μ§œμ™€ μ‹œκ°„μ„ μ½μ–΄μ˜€λŠ” λ©”μ„œλ“œ

date.getFullYear()
// 연도

date.getMonth()
// μ›” (주의! 0 = 1μ›”, 11 = 12μ›”)

date.getDate()
// 일

date.getDay()
// μš”μΌ (주의! 0 = μΌμš”μΌ, 6 = ν† μš”μΌ)

date.getHours()
// μ‹œ (주의! 0 = μƒˆλ²½ 12μ‹œ, 23 = μ˜€ν›„ 11μ‹œ)

date.getMinutes()
// λΆ„

date.getSeconds()
// 초

date.getMilliseconds()
// λ°€λ¦¬μ΄ˆ (주의! 1000ms = 1s)

date.getTime()
// 1970λ…„ 이후 경과된 μ‹œκ°„ (λ°€λ¦¬μ΄ˆ ν˜•νƒœλ‘œ ν‘œν˜„λ¨)

date.toGMTString(), date.toUTCString()
// GMT(κ·Έλ¦¬λ‹ˆμΉ˜ ν‰κ· μ‹œ)와 UTC(ν˜‘μ • μ„Έκ³„μ‹œ)λ₯Ό κΈ°μ€€μœΌλ‘œ ν•˜λŠ” λ‚ μ§œμ™€ μ‹œκ°„
// ex) "Fri, 17 Sep 2021 07:45:08 GMT"

date.toString();
// ex) "Fri Sep 17 2021 16:45:08 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)"

date.toTimeString();
// ex) "16:45:08 GMT+0900 (ν•œκ΅­ ν‘œμ€€μ‹œ)"

date.toLocaleString()
// ν˜„μž¬ μ§€μ—­μ˜ ν˜•μ‹μœΌλ‘œ ν‘œν˜„λœ λ‚ μ§œμ™€ μ‹œκ°„
// ex) λŒ€ν•œλ―Όκ΅­μ˜ 경우, "λ…„.μ›”.일. μ˜€μ „/μ˜€ν›„ μ‹œ:λΆ„:초" β†’ "2021. 9. 17. μ˜€ν›„ 4:45:08"

(3) Date 객체의 λ‚ μ§œμ™€ μ‹œκ°„μ„ μ„€μ •ν•˜λŠ” λ©”μ„œλ“œ

  • μœ„μ˜ get λ©”μ„œλ“œλ“€μ„ set λ©”μ„œλ“œλ‘œ λ°”κΏ” μ“Έ 수 있음
date.setFullYear(μ„€μ •ν•˜κ³  싢은 연도);

date.setMonth(μ„€μ •ν•˜κ³  싢은 μ›”);

date.setDate(μ„€μ •ν•˜κ³  싢은 일);

.
.
.

4) ν‚€μ›Œλ“œ in

객체에 ν•΄λ‹Ή ν”„λ‘œνΌν‹°κ°€ μ‘΄μž¬ν•˜λŠ”μ§€ 확인 (true/false)

var syong = {
  name : 'syong',
  age : 15,
  like : 'piano',
  hate : 'exercise'
};
console.log('hate' in syong); // true
console.log('hobby' in syong); // false

5) 객체 μƒμ„±μž ν•¨μˆ˜ & new μ—°μ‚°μž

new μ—°μ‚°μžμ™€ μƒμ„±μž ν•¨μˆ˜ μ°Έκ³ 

  • μž¬μ‚¬μš© κ°€λŠ₯ν•œ 객체 생성 μ½”λ“œ
  • (객체 λ¦¬ν„°λŸ΄ λ¬Έλ²•μœΌλ‘œ λ§Œλ“œλŠ” 것보닀) μœ μ‚¬ν•œ 객체 μ—¬λŸ¬ 개λ₯Ό 훨씬 μ‰½κ²Œ λ§Œλ“€ 수 μžˆλ‹€
// 객체 μƒμ„±μž ν•¨μˆ˜
function Circle(radius) {
  this.radius = radius;
}

var simpleCircle = new Circle(10); // new μ—°μ‚°μž μ΄μš©ν•΄ simpleCircleμ΄λΌλŠ” 객체 생성
console.log(simpleCircle);
// { radius : 10 }

var colouredCircle = new Circle(5); // new μ—°μ‚°μž μ΄μš©ν•΄ colouredCircleμ΄λΌλŠ” 객체 생성
colouredCircle.colour = 'red';
console.log(colouredCircle);
// { radius : 5, colour : 'red' }
  • new μ—°μ‚°μžλ₯Ό μ΄μš©ν•΄ Circle ν•¨μˆ˜μ— 10μ΄λΌλŠ” λ§€κ°œλ³€μˆ˜λ₯Ό μ „λ‹¬ν•˜μ—¬ μ‹€ν–‰ν•¨μœΌλ‘œμ¨ simpleCircle 객체λ₯Ό μƒμ„±ν•˜λŠ” κ³Όμ •
function Circle(radius) {
  // this = {} β†’ 빈 객체λ₯Ό λ§Œλ“€μ–΄ this에 ν• λ‹Ή (μ•”μ‹œμ μΈ κ³Όμ •)
  this.radius = radius; // this = { radius = 10 }
  // return this β†’ thisλ₯Ό λ°˜ν™˜ (μ•”μ‹œμ μΈ κ³Όμ •)
}

4. AboutMutability.js

1) μƒμ„±μž 및 μƒμ„±μž 인수 μ•ˆμ˜ λ³€μˆ˜λŠ” privateν•˜λ‹€

[TIL] 210915 δΈ­ 'private variable' λΆ€λΆ„ μ°Έκ³ 

(1) 문제 μ½”λ“œ

function Person(firstname, lastname)
{
  var fullName = firstname + " " + lastname;

  this.getFirstName = function () { return firstname; };
  this.getLastName = function () { return lastname; };
  this.getFullName = function () { return fullName; };
}
var aPerson = new Person ("John", "Smith");

aPerson.firstname = "Penny";
aPerson.lastname = "Andrews";
aPerson.fullName = "Penny Andrews";

expect(aPerson.getFirstName()).toBe(FILL_ME_IN);
expect(aPerson.getLastName()).toBe(FILL_ME_IN);
expect(aPerson.getFullName()).toBe(FILL_ME_IN);

aPerson.getFullName = function () {
  return aPerson.lastname + ", " + aPerson.firstname;
};

expect(aPerson.getFullName()).toBe(FILL_ME_IN);

(2) 풀이 및 해석

πŸ“Œλ₯Ό ν‘œμ‹œν•œ 뢀뢄이 이해λ₯Ό μœ„ν•΄ λ‚΄κ°€ λΌμ›Œ 넣은 μ½”λ“œμž„

// 객체 μƒμ„±μž ν•¨μˆ˜
function Person(firstname, lastname)
{
  var fullName = firstname + " " + lastname;

  this.getFirstName = function () { return firstname; };
  this.getLastName = function () { return lastname; };
  this.getFullName = function () { return fullName; };
}

// new μ—°μ‚°μž μ΄μš©ν•΄ aPersonμ΄λΌλŠ” 객체 생성
var aPerson = new Person ("John", "Smith");

// πŸ“Œ μ—¬κΈ°κΉŒμ§€ μ½”λ“œμ˜ κ²°κ³Ό
aPerson = {
  getFirstName : function () { return firstname; },
  getLastName : function () { return lastname; },
  getFullName : function () { return fullName; }
}

// 객체에 ν”„λ‘œνΌν‹° μΆ”κ°€ ❗
// 객체의 κΈ°μ‘΄ ν”„λ‘œνΌν‹°μ— 영ν–₯ ❌, κΈ°μ‘΄ ν”„λ‘œνΌν‹° κ°’ λ³€κ²½ ❌
aPerson.firstname = "Penny";
aPerson.lastname = "Andrews";
aPerson.fullName = "Penny Andrews";

// πŸ“Œ μ—¬κΈ°κΉŒμ§€ μ½”λ“œμ˜ κ²°κ³Ό
aPerson = {
  getFirstName: function () { return firstname; }, // return κ°’ "John"
  getLastName: function () { return lastname; }, // return κ°’ "Smith"
  getFullName: function () { return fullName; }, // return κ°’ "John Smith"
  firstname: "Penny",
  lastname: "Andrews",
  fullName: "Penny Andrews"
}

// κ·ΈλŸ¬λ―€λ‘œ
expect(aPerson.getFirstName()).toBe("John");
expect(aPerson.getLastName()).toBe("Smith");
expect(aPerson.getFullName()).toBe("John Smith");

// ν•œνŽΈ,
aPerson.getFullName = function () {
  return aPerson.lastname + ", " + aPerson.firstname;
};

// κ·ΈλŸ¬λ―€λ‘œ
expect(aPerson.getFullName()).toBe("Andrews, Penny");

✨ 내일 ν•  것

  1. javascript-koans WLs마무리
profile
λŠ₯λ™μ μœΌλ‘œ μ‚΄μž, ν–‰λ³΅ν•˜κ²ŒπŸ˜

0개의 λŒ“κΈ€