console
을 찍으면서 진행을 해보았다."비교연산자 '==='은 두 값의 일치 여부를 엄격하게 검사한다"
1 === 1 // true
1 === '1' // false
변수를 선언할 때에는 let
,const
,var
를 사용할 수 있다.
var
는 중복 선언이 가능하여 오륲가 발생할 수 있어서 let
, const
에 비해 잘 사용되지 않는다.
let
과 const
의 차이는 재할당 여부이다. 코드의 가독성을 높이고 오류방지를 위해 고정값으로 const를 사용해주는 것이 좋다.
const
로 선언된 변수에는 재할당이 금지된다.
const
로 선언된 배열의 경우, 새로운 요소를 추가하거나 삭제할 수 있다.
const
로 선언된 객체의 경우, 속성을 추가하거나 삭제할 수 있다.
function () {
let funcExpressed = 'it is function';
typeof funcDeclared // 'function'
typeof funcExpressed // 'string'
function funcDeclared() {
return 'this is a function declaration';
}
let sum = (a, b) => a + b;
< 함수 func는 화살표(=>) 우측의 표현식을 평가하고, 평가 결과를 반환한다 >
let overTwenty = true;
let allowedToDrink = overTwenty;
overTwenty = false;
expect(overTwenty).to.equal(false);
expect(allowedToDrink).to.equal(true);
let variable = 'variable';
let variableCopy = 'variableCopy';
variableCopy = variable;
variable = variableCopy;
expect(variable).to.equal(variableCopy);
참조 자료형의 데이터는 동적으로 변한다.
참조 자료형을 변수에 할당할 경우, 데이터의 주소가 저장된다.
const overTwenty = ['A', 'B', 'C'];
let allowedToDrink = overTwenty;
overTwenty.push('D');
expect(allowedToDrink).to.deep.equal(overTwenty);
overTwenty[1] = 'E';
expect(allowedToDrink[1]).to.deep.equal(E[1]);
`` ``````````
const arr = ['candy', 'butter', 'and', 'gum'];
arr.slice(0) = ['candy', 'butter', 'and', 'gum'];
// 위 코드는 원본을 건드리지 않고 arr전체를 복사한다
this
는 method를 호출한 시점에 method를 호출한 객체를 말한다.let obj = {foo: function() {return this}};
function () {
const megalomaniac = {
mastermind: 'Brain',
henchman: 'Pinky',
getFusion: function () {
return this.henchman + this.mastermind;
},
battleCry(numOfBrains) {
return `They are ${this.henchman} and the` + ` ${this.mastermind}`.repeat(numOfBrains);
},
};
< 대상이 객체인 경우 >
const myObject1 = {
laptop: 'MacBook Pro',
tablet: 'iPad Pro'
}
const myObj2 = {...myObj1};
console.log(myObj1); // {laptop: "MacBook Pro", tablet: "iPad Pro"}
console.log(myObj2); // {laptop: "MacBook Pro", tablet: "iPad Pro"}
console.log(myObj1 === myObj2); // false
< 대상이 배열인 경우 >
const myArray1 = ['one', 'two', 'three', 'four', 'five'];
const myArray2 = [...myArray1];
console.log(myArray1); // ["one", "two", "three", "four", "five"]
console.log(myArray2); // ["one", "two", "three", "four", "five"]
console.log(myArray1 === myArray2); // false
let [a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20
< 배열을 분리한다 >
const array = ['I', 'can', 'do', 'it']
const [first, second] = array
expect(first).to.eql('I')
expect(second).to.eql('can')
const result = []
function foo([first, second]) {
result.push(second)
result.push(first)
foo(array)
expect(result).to.eql(['can', 'I'])
}
< rest/spread 문법을 배열 분해에 적용할 수 있다 >
const array = ['I', 'can', 'do', 'it']
const [start, ...rest] = array
expect(start).to.eql('I')
expect(rest).to.eql(['can', 'do', 'it'])
오늘은 페어랑 함께 'javascript koans' 과제를 진행하였다. 약 3주간의 학습을 했던 내용을 간단한 테스트 문제로 총 9개주제로 이뤄진 문제를 풀어보았는데 생각보다 쉽지는 않았다. 복습임에도 불구하고 막혔던 부분도 존재하였고 (scope에 클로저함수라는건 안비밀...) 쉽게쉽게 풀려나가는 부분도 있었다. section1이 끝나가는 도중에 복습을 하며 리마인드 한다는 것이 꽤나 유익하고 좋았던 것 같다. 오늘 과제를 진행하였던걸 바탕으로 막혔던 부분은 이번 연휴에 시간을 내서 다시 한 번더 복습을 해봐야겠다!