array.slice() / μμ½μ΄ arguments / κΈΈμ΄λ₯Ό κ°λ λΉ λ°°μ΄ / array.join() / Date() / ν€μλ in / κ°μ²΄ μμ±μ ν¨μ & new μ°μ°μ / private variable
javascript-koans νμ΄
νλ©΄μ λͺ¨λ₯΄κ±°λ ν·κ°λ Έλ λΆλΆ μ 리
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μ΄ λ°°μ΄μ κΈΈμ΄μ κ°μ κ²½μ°μλ 'λΉ λ°°μ΄'μ μΆλ ₯νλ€
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');
var array = new Array(5);
console.log(array);
// [undefined, undefined, undefined, undefined, undefined]
(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'
νμ¬ λ μ§μ μκ°μ μ½μ΄μ€κ±°λ μ€μ ν μ μλ€
(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 κ°μ²΄μ λ μ§μ μκ°μ μ€μ νλ
λ©μλ
date.setFullYear(μ€μ νκ³ μΆμ μ°λ);
date.setMonth(μ€μ νκ³ μΆμ μ);
date.setDate(μ€μ νκ³ μΆμ μΌ);
.
.
.
κ°μ²΄μ ν΄λΉ νλ‘νΌν°κ° μ‘΄μ¬νλμ§ νμΈ (true/false)
var syong = {
name : 'syong',
age : 15,
like : 'piano',
hate : 'exercise'
};
console.log('hate' in syong); // true
console.log('hobby' in syong); // false
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' }
function Circle(radius) {
// this = {} β λΉ κ°μ²΄λ₯Ό λ§λ€μ΄ thisμ ν λΉ (μμμ μΈ κ³Όμ )
this.radius = radius; // this = { radius = 10 }
// return this β thisλ₯Ό λ°ν (μμμ μΈ κ³Όμ )
}
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");