LEARNING JAVASCRIPT - Trang 172

sense of security by anecdotal testing: never assume a given order of enumeration for

properties.
With that warning out of the way, let’s now consider the primary ways to enumerate

an object’s properties.

for...in

The traditional way to enumerate the properties of an object is

for...in

. Consider

an object that has some string properties and a lone symbol property:

const

SYM

=

Symbol

();

const

o

=

{

a

:

1

,

b

:

2

,

c

:

3

, [

SYM

]

:

4

};

for

(

let

prop

in

o

) {

if

(

!

o

.

hasOwnProperty

(

prop

))

continue

;

console.log(

`

${

prop

}

:

${

o

[

prop

]

}

`

);

}

This seems pretty straightforward…except you are probably reasonably wondering

what

hasOwnProperty

does. This addresses a danger of the

for...in

loop that won’t

be clear until later in this chapter: inherited properties. In this example, you could

omit it, and it wouldn’t make a difference. However, if you’re enumerating the prop‐

erties of other types of objects—especially objects that originated elsewhere—you

may find properties you didn’t expect. I encourage you to make it a habit to use

hasOwnProperty

. We’ll soon learn why it’s important, and you’ll have the knowledge

to determine when it’s safe (or desirable) to omit.
Note that the

for...in

loop doesn’t include properties with symbol keys.

While it’s possible to use for...in to iterate over an array, it’s gen‐

erally considered a bad idea. I recommend using a regular for loop

or forEach for arrays.

Object.keys

Object.keys

gives us a way to get all of the enumerable string properties of an object

as an array:

const

SYM

=

Symbol

();

const

o

=

{

a

:

1

,

b

:

2

,

c

:

3

, [

SYM

]

:

4

};

Object

.

keys

(

o

).

forEach

(

prop

=>

console.log(

`

${

prop

}

:

${

o

[

prop

]

}

`

));

148 | Chapter 9: Objects and Object-Oriented Programming

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.