Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Enumerability and Iteration | Understanding and Working with Objects
Objects and Prototypes in JavaScript

bookEnumerability and Iteration

Understanding enumerable properties is crucial for controlling how your object's properties are accessed and manipulated in JavaScript. Properties in JavaScript objects have an enumerable attribute, which determines whether a property shows up during property enumeration, such as when using a for...in loop or Object.keys. By default, properties you add directly to an object are enumerable, meaning they are visible during iteration and methods that list object keys. However, sometimes you may want to have properties that are not visible during enumeration, such as internal values or helper functions. This is where controlling enumerability becomes valuable: it allows you to hide certain properties from loops and methods that operate on object keys, making your objects more robust and predictable.

12345678910111213141516
const user = { name: "Alice" }; // Add an enumerable property Object.defineProperty(user, "age", { value: 30, enumerable: true }); // Add a non-enumerable property Object.defineProperty(user, "password", { value: "secret123", enumerable: false }); console.log(JSON.stringify(user)); // { name: 'Alice', age: 30 } console.log(user.password); // 'secret123'
copy

With these properties in place, you can see how enumeration works in practice. When you use the for...in loop or Object.keys, only enumerable properties are included. In the user object above, "name" and "age" are enumerable, but "password" is not. This means "password" will not appear when you iterate through the object's properties. This behavior is important for protecting sensitive information or internal implementation details from being exposed unintentionally.

123456789101112131415161718192021222324
const user = { name: "Alice" }; // Add an enumerable property Object.defineProperty(user, "age", { value: 30, enumerable: true }); // Add a non-enumerable property Object.defineProperty(user, "password", { value: "secret123", enumerable: false }); // Using for...in to iterate over properties for (let key in user) { console.log(key); // Outputs: name, age } // Using Object.keys to get enumerable property names console.log(Object.keys(user)); // ['name', 'age'] // Non-enumerable property "password" is not included console.log(user.password); // 'secret123'
copy
question mark

Which of the following statements about enumerable and non-enumerable properties are correct?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain how to make a property non-enumerable after it has already been added to an object?

What are some common use cases for non-enumerable properties in JavaScript?

How can I list all properties of an object, including non-enumerable ones?

Awesome!

Completion rate improved to 7.69

bookEnumerability and Iteration

Свайпніть щоб показати меню

Understanding enumerable properties is crucial for controlling how your object's properties are accessed and manipulated in JavaScript. Properties in JavaScript objects have an enumerable attribute, which determines whether a property shows up during property enumeration, such as when using a for...in loop or Object.keys. By default, properties you add directly to an object are enumerable, meaning they are visible during iteration and methods that list object keys. However, sometimes you may want to have properties that are not visible during enumeration, such as internal values or helper functions. This is where controlling enumerability becomes valuable: it allows you to hide certain properties from loops and methods that operate on object keys, making your objects more robust and predictable.

12345678910111213141516
const user = { name: "Alice" }; // Add an enumerable property Object.defineProperty(user, "age", { value: 30, enumerable: true }); // Add a non-enumerable property Object.defineProperty(user, "password", { value: "secret123", enumerable: false }); console.log(JSON.stringify(user)); // { name: 'Alice', age: 30 } console.log(user.password); // 'secret123'
copy

With these properties in place, you can see how enumeration works in practice. When you use the for...in loop or Object.keys, only enumerable properties are included. In the user object above, "name" and "age" are enumerable, but "password" is not. This means "password" will not appear when you iterate through the object's properties. This behavior is important for protecting sensitive information or internal implementation details from being exposed unintentionally.

123456789101112131415161718192021222324
const user = { name: "Alice" }; // Add an enumerable property Object.defineProperty(user, "age", { value: 30, enumerable: true }); // Add a non-enumerable property Object.defineProperty(user, "password", { value: "secret123", enumerable: false }); // Using for...in to iterate over properties for (let key in user) { console.log(key); // Outputs: name, age } // Using Object.keys to get enumerable property names console.log(Object.keys(user)); // ['name', 'age'] // Non-enumerable property "password" is not included console.log(user.password); // 'secret123'
copy
question mark

Which of the following statements about enumerable and non-enumerable properties are correct?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
some-alt