Koans.
Scope.
declaration
)๊ณผ ํจ์ ํํ์(expression
)์ ์ฐจ์ด๋ฅผ ํ์ธํฉ๋๋ค.
let funcExpressed = "to be a function";
expect(typeof funcDeclared).to.equal("function"); // ์ ์ธ์ ํจ์๋ function
expect(typeof funcExpressed).to.equal("string"); // let funcExpressed์ผ๋ก ์ ์ธ์ด ๋จผ์ ๋์ด์.
function funcDeclared() {
return "this is a function declaration";
}
funcExpressed = function () {
return "this is a function expression";
};
const funcContainer = { func: funcExpressed }; // funcContainer๋ funcContainer ๊ฐ์ฒด๋ด์ func์ ํธ์ถ..?
expect(funcContainer.func()).to.equal("this is a function expression"); // ์ด๊ธฐ๊ฐ์ธ funcExpressed์ ์ฌํ ๋น.
funcContainer.func = funcDeclared;
expect(funcContainer.func()).to.equal("this is a function declaration"); // funcDeclared ๋ฆฌํด๊ฐ ํ ๋น
default parameter
์ ๋ํด ํ์ธํฉ๋๋ค. function defaultParameter(num = 5) { // ๋งค๊ฐ๋ณ์ ์์ ๊ฐ์ด ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ 5.
return num;
}
expect(defaultParameter()).to.equal(5); // ๊ธฐ๋ณธ๊ฐ 5 ํ ๋น
expect(defaultParameter(10)).to.equal(10); // ๋งค๊ฐ๋ณ์๋ก 10์ ์ฃผ์์ผ๋ฏ๋ก 10์ด ์ถ๋ ฅ
function pushNum(num, arr = []) { // num์ ๋ฐฐ์ด(arr)๋ก ์ถ๋ ฅ
arr.push(num);
return arr;
}
expect(pushNum(10)).to.deep.equal([10]);
expect(pushNum(20)).to.deep.equal([20]);
expect(pushNum(4, [1, 2, 3])).to.deep.equal([1, 2, 3, 4]);
let age = 27;
let name = "jin";
let height = 179;
function outerFn() {
let age = 24;
name = "jimin";
let height = 178;
function innerFn() {
age = 26;
let name = "suga"; //์ ์ธ๋ ๋ณ์๋ ๊ทธ ํจ์ ๋ด์์๋ง ์ฌ์ฉํ ์ ์๋ค,
return height;
}
innerFn();
expect(age).to.equal(26);
expect(name).to.equal("jimin");
return innerFn;
}
const innerFn = outerFn();
expect(age).to.equal(27);
expect(name).to.equal("jimin");
expect(innerFn()).to.equal(178);
Primitive & Reference
const overTwenty = ["hongsik", "minchul", "hoyong"];
let allowedToDrink = overTwenty;
overTwenty.push("san");
expect(allowedToDrink).to.deep.equal([
"hongsik",
"minchul",
"hoyong",
"san",
]);
overTwenty[1] = "chanyoung";
expect(allowedToDrink[1]).to.deep.equal("chanyoung");
const ages = [22, 23, 27];
allowedToDrink = ages;
expect(allowedToDrink === ages).to.equal(true); //๋ ๋ณ์๊ฐ ๊ฐ์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๊ณ ์๊ธฐ ๋๋ฌธ.
expect(allowedToDrink === [22, 23, 27]).to.equal(false); //๋ฐฐ์ด์ ๋ณต์ฌํ์ง ์๊ณ ์ฐธ์กฐ๋ง ํ๊ณ ์๊ธฐ ๋๋ฌธ.
const nums1 = [1, 2, 3];
const nums2 = [1, 2, 3];
expect(nums1 === nums2).to.equal(false);
const person = {
son: {
// === boy
age: 9, // 20
},
};
const boy = person.son;
boy.age = 20;
expect(person.son.age).to.equal(20);
expect(person.son === boy).to.equal(true);
expect(person.son === { age: 9 }).to.equal(false);
expect(person.son === { age: 20 }).to.equal(false);
[1, 2, 3];
: [1, 2, 3]์ด๋ผ๋ ๋ฐ์ดํฐ๊ฐ heap์ ์ ์ฅ๋์ง๋ง ๋ณ์ ํ ๋น์ด ๋์ง ์์ ์ฃผ์๋ ์ด๋์๋ ์ ์ฅ๋์ง ์๋๋ค.const num1 = [1, 2, 3];
: [1, 2, 3]์ด๋ผ๋ ๋ฐ์ดํฐ๊ฐ heap์ ์ ์ฅ๋๊ณ , ๊ทธ ์ฃผ์๊ฐ ๋ณ์ num1์ ์ ์ฅ๋๋ค.const num2 = [1, 2, 3];
: [1, 2, 3]์ด๋ผ๋ ๋ฐ์ดํฐ๊ฐ heap์ ์ ์ฅ๋๊ณ , ๊ทธ ์ฃผ์๊ฐ ๋ณ์ num2์ ์ ์ฅ๋๋ค.1), 2), 3)์์ ๋งํ๋ ์ฃผ์๋ ์ ๋ถ ๋ค๋ฅธ ์ฃผ์์
๋๋ค.
Array
Array
์ ๊ธฐ๋ณธ์ ํ์ธํฉ๋๋ค.const emptyArr = [];
expect(typeof emptyArr === "array").to.equal(false);
expect(emptyArr.length).to.equal(0);
const multiTypeArr = [
0,
1,
"two",
function () {
return 3;
},
{ value1: 4, value2: 5 },
[6, 7],
];
expect(multiTypeArr.length).to.equal(6); // [0, 1, 'two', ฦ, {โฆ}, Array(2)]
expect(multiTypeArr[0]).to.equal(0);
expect(multiTypeArr[2]).to.equal("two");
expect(multiTypeArr[3]()).to.equal(3);
expect(multiTypeArr[4].value1).to.equal(4);
expect(multiTypeArr[4]["value2"]).to.equal(5);
expect(multiTypeArr[5][1]).to.equal(7);
Object
const emptyObj = {};
expect(typeof emptyObj === "object").to.equal(true);
expect(emptyObj.length).to.equal(undefined); // Object.keys()๋ฅผ ์ฌ์ฉํด์ ๋ฐฐ์ด๋ก ๋ฐํ ํ .length๋ก ๊ธธ์ด ํ์ธ
const megalomaniac = {
mastermind: "Joker",
henchwoman: "Harley",
getMembers: function () {
return [this.mastermind, this.henchwoman];
},
relations: ["Anarky", "Duela Dent", "Lucy"],
twins: {
"Jared Leto": "Suicide Squad",
"Joaquin Phoenix": "Joker",
"Heath Ledger": "The Dark Knight",
"Jack Nicholson": "Tim Burton Batman",
},
};
expect(megalomaniac.length).to.equal(undefined);
expect(megalomaniac.mastermind).to.equal("Joker");
expect(megalomaniac.henchwoman).to.equal("Harley");
expect(megalomaniac.henchWoman).to.equal(undefined);
expect(megalomaniac.getMembers()).to.deep.equal(["Joker", "Harley"]);
expect(megalomaniac.relations[2]).to.equal("Lucy");
expect(megalomaniac.twins["Heath Ledger"]).to.deep.equal("The Dark Knight");
Destructuring
rest/spread
๋ฌธ๋ฒ์ ๊ฐ์ฒด ๋ถํด์ ์ ์ฉํ ์ ์์ต๋๋ค #3const user = {
name: "๊น์ฝ๋ฉ",
company: {
name: "Code States",
department: "Development",
role: {
name: "Software Engineer",
},
},
age: 35,
};
const changedUser = {
...user,
name: "๋ฐํด์ปค",
age: 20,
};
const overwriteChanges = {
name: "๋ฐํด์ปค",
age: 20,
...user, //spread ๋ฌธ์ด ๋ค์ ์ค๋ฉด ์์ ํ ๋น๋ ๋ฌธ๋ฒ์ ์ด๊ธฐํ
};
const changedDepartment = {
...user,
company: {
...user.company,
department: "Marketing",
},
};
expect(changedUser).to.eql({
name: "๋ฐํด์ปค",
company: {
name: "Code States",
department: "Development",
role: {
name: "Software Engineer",
},
},
age: 20,
});
expect(overwriteChanges).to.eql({
name: "๊น์ฝ๋ฉ",
company: {
name: "Code States",
department: "Development",
role: {
name: "Software Engineer",
},
},
age: 35,
});
expect(changedDepartment).to.eql({
name: "๊น์ฝ๋ฉ",
company: {
name: "Code States",
department: "Development",
role: {
name: "Software Engineer",
},
},
company: {
name: "Code States",
department: "Marketing",
role: {
name: "Software Engineer",
},
},
age: 35,
});
์ฐธ์กฐ