
ํ ๋ฐฉ ์ ๋ฆฌ
| ๋ป | ํจ์ | ๋ป | ํ์ | ์์ | |
| Iterable | ๋ฐ๋ณต ๊ฐ๋ฅํ | for (value of arr) | ์ํํ๋ฉด์ ๊ฐ์ ์ ๊ทผํ ์ ์๋๊ฐ | iterator ๊ฐ์ฒด๊ฐ ์๋ ๋ฐ์ดํฐ ํ์ | Array, map, set, nodeList, String |
| Enumerable | ์ด๊ฑฐ ๊ฐ๋ฅํ | for ( key in obj) | ๊ฐ์ฒด์ key์ ์ ๊ทผํ ์ ์๋๊ฐ | ๊ฐ์ฒด ํ์ | Object |
์์ ๋ก ์ดํด๋ณด์
// ๋ฐฐ์ด ์์
let arr = ['Junha', 'Rany', 'Leo']
arr.hobit = 'Prodo'
// ๊ฐ์ฒด ์์
let obj = {
firstKey: ['Nunu','Garen'],
secondKey:['Yumi','Galio','Ryze'],
thirdKey:['Minsu','heejung']
}
obj.fourthKey = 'Junha'
1. for in ์ key์ ์ ๊ทผํ๋ค.
for (let key in arr) {
console.log(key)
}
// 0
// 1
// 2
// hobit
for (let key in obj) {
console.log(key)
}
// firstKey
// secondKey
// thirdKey
// fourthKey
2. for of ๋ value์ ์ ๊ทผํ๋ค.
- for of๋ฅผ ๋๊ธฐ ์ํด์ iterator ๊ฐ์ฒด๊ฐ ์์ด์ผ ํ๋ค.
- Iterable ๊ฐ์ฒด๋ ์ ์ฌ๋ฐฐ์ด๊ฐ์ฒด์ ์กด์ฌํ๋ค.
for (let value of arr) {
console.log(value)
}
// Junha
// Rany
// Leo
// Prodo (์ถ๋ ฅx)
hobit ํ๋กํผํฐ๋ key๊ฐ 012.. ์์ผ๋ก ๋ค์ด๊ฐ ์๋ ๋ฐฐ์ด ์์๋ฅผ ๊ฐ์ถ ํํ๊ฐ ์๋๋ค.
์ด๋ iterable ํ์ง ์๋ค๋ ๋ป์ด๊ธฐ์, for of ๋ฐ๋ณต๋ฌธ์์ ์ ์ธ๋๋ค.
for (let value of obj) {
console.log(value)
}
// TypeError: obj is not iterable
object๋ iterator๊ฐ ์๊ธฐ ๋๋ฌธ์ for of ๋ฐ๋ณต๋ฌธ์ ๋ ์ ์๋ค.
iterableํ๊ฒ ์ค์ ํ๋ ค๋ฉด cutstom configuration์ด ํ์ํ๋ค.
for (let value of firstKey.town) {
console.log(value)
}
// Nunu
// Garen
obj.firstKey๋ ๋ฐฐ์ด์ด๋ค.
๋ฐฐ์ด์ iterableํ๊ธฐ ๋๋ฌธ์ for of ๋ฐ๋ณต๋ฌธ์ ๋ ์ ์๋ค.
for (let value of obj.fourthKey) {
console.log(value)
}
// J
// u
// n
// h
// a
string๋ ๋ง์ฐฌ๊ฐ์ง๋ก iterableํ๊ธฐ ๋๋ฌธ์, for of ๋ฐ๋ณต๋ฌธ์ ๋ ์ ์๋ค.
๋ฉ์๋ ๋ฐ ํ๋กํผํฐ
1. enumerable ์์ฑ
- for..in ๋ฐ๋ณต๋ฌธ์ด๋, Object.keys()๋ฅผ ํตํด ์ค๋ธ์ ํธ ์์ฑ์ ์ ๊ทผํ ์ ์๋์ง ์ ์ํ๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ์์ฑ์ enumerable:true๋ก ์ด๊ธฐํ๋๋ค
const yoonjung = {
home: 'seoul',
type: 'isfp'
}
yoonjung.major = 'sprots'
for (const key in yoonjung) {
console.log(key)
}
// home
// type
// major
2. Object.defineProperty()
ํน์ ํ๋๋ฅผ enumerable์ ํด๋น๋์ง ์๊ฒ ์ค์ ํ ์๋ ์๋ค.
enumerable:false๋ก ์ค์ ํ๋ฉด, Object.keys์ ๋ฐํ์ด ์๋๊ณ for in ๋ฐ๋ณต๋ฌธ์ ๋์ง๋ ์๋๋ค.
Object.defineProperty(obj, 'key', {
enumerable: false,
configurable: false,
writable: false,
value: 'static'
});
Object.defineProperty(yoonjung, 'height', {
enumerable: false,
value: 170
});
for (const key in person) {
console.log(key);
}
// home
// type
// major
// height๋ ์๋์ด
3. object.propertyIsEnumerable
ํด๋น ํ๋๊ฐ enumerableํ์ง ํ์ธํ ์ ์๋ค.
console.log(yoonjung.propertyIsEnumerable('home')); // => true
console.log(yoonjung.propertyIsEnumerable('type')); // => true
console.log(yoonjung.propertyIsEnumerable('major')); // => true
console.log(yoonjung.propertyIsEnumerable('height')); // => false
4. Object.keys(obj) : array
returns all enumerable own properties
var a = {};
Object.defineProperties(a, {
one: {enumerable: true, value: 1},
two: {enumerable: false, value: 2},
});
Object.getOwnPropertyNames(a); // ["one", "two"]
Object.keys(a); // ["one"]
5. Object.getOwnPropertyNames(obj) : array
returns all own properties of the object.
enumerable ์์ฑ ๊ฐ์ ์๊ด์์ด, ์ค๋ธ์ ํธ์ ๋ชจ๋ ํ๋กํผํฐ๋ฅผ ๋ฐํํ๋ค.
6. Object.prototype.hasOwnProperty(property) : boolean
๊ฐ์ฒด๊ฐ ํน์ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋์ง๋ฅผ ๋ํ๋ด๋ ๋ถ๋ฆฌ์ธ ๊ฐ์ ๋ฐํํ๋ค.
o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop'); // returns true
o.hasOwnProperty('toString'); // returns false
o.hasOwnProperty('hasOwnProperty'); // returns false
7. summary
- A property is enumerable if it has the enumerable attribute sets to true.
- The obj.propertyIsEnumerable() determines whether or not a property is enumerable.
- A property created via a simple assignment or a property initializer is enumerable.
'๐ Front > ๐ฑ Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ถ๋ณ๊ฐ๊ณผ ๊ฐ๋ณ๊ฐ์ ๋๋๋ ๊ธฐ์ค์ ๋ญ๊น? (0) | 2022.01.07 |
|---|---|
| ์คํฌ๋ฆฝํธ(script) ์ถ๊ฐ/์ ๊ฑฐํ๋ ๋ฒํผ ๋ง๋ค๊ธฐ (0) | 2021.11.22 |
| arguments ๊ฐ์ฒด: function์์ ์๋ ๋, ๋๊ฐ ์ ์ธํ๋? (0) | 2021.11.18 |
| [addEventListener] ์ฝ๋ฐฑํจ์์๊ฒ ์ธ์๋ฅผ ์ฃผ๋ ๋ฐฉ๋ฒ (0) | 2021.11.17 |
| key์ ์ ๊ทผํ๋ 2๊ฐ์ง ๋ฐฉ๋ฒ. obj.key, obj['key'] (0) | 2021.08.03 |