์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- ๋ธ๋์น ํ์ธ
- ์ฝ๋๋ธ๋ญ
- branch ์ญ์
- ![rejected]
- Git๋ช ๋ น์ด
- ๋ธ๋์น ์ญ์
- ์ฝ๋๋ธ๋ก
- markdown
- ๋ธ๋์น ์์ฑ
- ๋งํฌ๋ค์ด
- branch ์์ฑ
- branch ํ์ธ
Archives
- Today
- Total
DevLog
๊ฐ์ฒด koans ๋ณธ๋ฌธ
describe('Object์ ๋ํด์ ํ์ตํฉ๋๋ค.', function () {
it('Object์ ๊ธฐ๋ณธ์ ํ์ธํฉ๋๋ค.', function () {
const emptyObj = {};
expect(typeof emptyObj === 'object').to.equal(true);
expect(emptyObj.length).to.equal(undefined);
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');
});
it('Object์ ์์ฑ(property)๋ฅผ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ์ ํ์ธํฉ๋๋ค.', function () {
const megalomaniac = { mastermind: 'Agent Smith', henchman: 'Agent Smith' };
expect('mastermind' in megalomaniac).to.equal(true);
megalomaniac.mastermind = 'Neo';
expect(megalomaniac['mastermind']).to.equal('Neo');
expect('secretary' in megalomaniac).to.equal(false);
megalomaniac.secretary = 'Agent Smith';
expect('secretary' in megalomaniac).to.equal(true);
delete megalomaniac.henchman;
expect('henchman' in megalomaniac).to.equal(false);
});
it("'this'๋ method๋ฅผ ํธ์ถํ๋ ์์ ์ ๊ฒฐ์ ๋ฉ๋๋ค.", function () {
const currentYear = new Date().getFullYear();
const megalomaniac = {
mastermind: 'James Wood',
henchman: 'Adam West',
birthYear: 1970,
calculateAge: function (currentYear) {
return currentYear - this.birthYear;
},
changeBirthYear: function (newYear) {
this.birthYear = newYear;
},
};
expect(currentYear).to.equal(2021);
expect(megalomaniac.calculateAge(currentYear)).to.equal(51);
megalomaniac.birthYear = 2000;
expect(megalomaniac.calculateAge(currentYear)).to.equal(21);
megalomaniac.changeBirthYear(2010);
expect(megalomaniac.calculateAge(currentYear)).to.equal(11);
/*
object method๋ ๊ฐ์ฒด์ ์์ฑ์ผ๋ก ์ ์๋ ํจ์๋ฅผ ๋งํฉ๋๋ค.
๋ฐ๋ผ์ method์ ํธ์ถ์ 'object.method()'์ ํํ๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค.
'this'๋ method๋ฅผ ํธ์ถ๋๋ ์์ ์ method๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํต๋๋ค.
*/
});
it('๊ฐ์ฒด์ method๋ฅผ ์ ์ํ๋ ๋ฐฉ๋ฒ์ ํ์ธํฉ๋๋ค.', 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);
},
};
expect(megalomaniac.getFusion()).to.deep.equal('PinkyBrain');
expect(megalomaniac.battleCry(3)).to.deep.equal('They are Pinky and the Brain Brain Brain');
});
it('Object๋ฅผ ํจ์์ ์ธ์๋ก ์ ๋ฌํ ๊ฒฝ์ฐ, reference๊ฐ ์ ๋ฌ๋ฉ๋๋ค.', function () {
const obj = {
mastermind: 'Joker',
henchwoman: 'Harley',
relations: ['Anarky', 'Duela Dent', 'Lucy'],
twins: {
'Jared Leto': 'Suicide Squad',
'Joaquin Phoenix': 'Joker',
'Heath Ledger': 'The Dark Knight',
'Jack Nicholson': 'Tim Burton Batman',
},
};
function passedByReference(refObj) {
refObj.henchwoman = 'Adam West';
}
passedByReference(obj);
expect(obj.henchwoman).to.equal('Adam West');
const assignedObj = obj;
assignedObj['relations'] = [1, 2, 3];
expect(obj['relations']).to.deep.equal([1, 2, 3]);
const copiedObj = Object.assign({}, obj);
copiedObj.mastermind = 'James Wood';
expect(obj.mastermind).to.equal('Joker');
obj.henchwoman = 'Harley';
expect(copiedObj.henchwoman).to.equal('Adam West');
delete obj.twins['Jared Leto'];
expect('Jared Leto' in copiedObj.twins).to.equal(false);
/*
๋ง์ง๋ง ํ
์คํธ ์ฝ๋์ ๊ฒฐ๊ณผ๊ฐ ์์๊ณผ๋ ๋ฌ๋์ ์๋ ์์ต๋๋ค.
'Object.assign'์ ํตํ ๋ณต์ฌ๋ reference variable์ ์ฃผ์๋ง ๋ณต์ฌํ๊ธฐ ๋๋ฌธ์
๋๋ค.
์ด์ ๊ด๋ จํ์ฌ ์์ ๋ณต์ฌ(shallow copy)์ ๊น์ ๋ณต์ฌ(deep copy)์ ๋ํด์ ํ์ตํ์๊ธฐ ๋ฐ๋๋๋ค.
๊ฐ์ด๋๊ฐ ๋ ๋งํ ํ์ต์๋ฃ๋ฅผ ์ฒจ๋ถํฉ๋๋ค.
https://scotch.io/bar-talk/copying-objects-in-javascript
https://medium.com/watcha/๊น์-๋ณต์ฌ์-์์-๋ณต์ฌ์-๋ํ-์ฌ๋์๋-์ด์ผ๊ธฐ-2f7d797e008a
*/
});
});
'๐ง๐ปโ๐ป ๊ฐ๋ฐ๊ฐ๋ฐ > javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๊ฐ์ฒด๋ฅผ ์ฝ์์ฐฝ์ ์ถ๋ ฅํ๋ ๋ฒ (0) | 2021.05.31 |
---|---|
ํ์ดํ ํจ์ (0) | 2021.05.30 |
๋ฐฐ์ด ์์ ์ถ๊ฐ ๋ฐ ์ญ์ , ๋ฐฐ์ด ์์ ํฌํจ ์ฌ๋ถ ํ์ธ/ koans (0) | 2021.05.27 |
Parameter & Argument์ ์ฐจ์ด์ (0) | 2021.05.24 |
๋ณ์ (0) | 2021.05.11 |
Comments