Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 7.69

bookEnumerability and Iteration

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 5
some-alt