Non-enumerable Property
Property không xuất hiện khi duyệt object bằng Object.keys() hoặc for...in loop.
Tạo non-enumerable property
Dùng Object.defineProperty() với enumerable: false.
Ví dụ
<html> <body> <script> var person = { name: 'gopal' }; person.age = '21'; person['country'] = 'India'; Object.defineProperty(person, 'salary', { value: '80,000$', enumerable: false }); document.write(Object.keys(person)); </script> </body> </html>
Kết quả
name,age,country
Property salary không xuất hiện trong Object.keys() do enumerable: false. Cũng dùng để tạo read-only property. Để làm enumerable: set enumerable: true.