
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the importance of "enumerable" attribute in defining a property in JavaScript object?
We can define a property of an object using Dot and Bracket notations. There is also another way in which a property called Object.defineProperty() is used to define a property. It usually takes 3 parameters they are object name, property name, property descriptor.
syntax
Object.defineProperty(object name, property name, property descriptor)
Lets' define a property with this method.
Example
In the following example, initially, the object has only one property named 'one'. Later on, another property named 'two' is added. Now when we tried to display all the properties, only the first property was displayed but not the added property as shown in the output.
<html> <body> <script> var object = {one: 1}; Object.defineProperty( object, 'two', { value: 2 } ); document.write(JSON.stringify(object)); </script> </body> </html>
Output
{"one":1}
This is all because of the "enumerable" attribute. "enumerable" attribute must be true to define a property in an object. But, "enumerable" takes up a value "false" when not declared. So, to make the value as "true" we have to declare "enumerable" and need to assign "true" to it.
In the following example when "enumerable" is initialized and assigned value as true, all the properties were displayed as shown in the output.
Example
<html> <body> <script> var object = {one: 1}; Object.defineProperty( object, 'two', { value: 2, enumerable: true } ); document.write(JSON.stringify(object)); </script> </body> </html>
Output
{"one":1,"two":2}
- Related Articles
- What is the importance of Navigator Object in JavaScript?
- What is non-enumerable property in JavaScript and how can it be created?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- What is a NaN property of a Number object in JavaScript?
- The Importance of Defining Project Acceptance Criteria
- What is the importance of Symbol.isConcatSpreadable in JavaScript?
- What is the importance of Math.trunc() method in JavaScript?
- What is the importance of _isEqual() method in JavaScript?
- What is the importance of _without() method in JavaScript?
- What is the importance of _.initial() function in JavaScript?
- What is the importance of _.union() method in JavaScript?
- What is the importance of str.padStart() method in JavaScript?
- What is an object in JavaScript and access any property ?**
- Defining a primary key in an Attribute view in SAP HANA
- What is importance of startsWith() method in JavaScript?
