๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

 

ํ•œ ๋ฐฉ ์ •๋ฆฌ

  ๋œป ํ•จ์ˆ˜ ๋œป ํƒ€์ž… ์˜ˆ์‹œ
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.

 

 

๋ฐ˜์‘ํ˜•