Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get Property Descriptors of an Object in JavaScript?
In JavaScript, property descriptors define the behavior and characteristics of object properties. The Object.getOwnPropertyDescriptor() method returns a descriptor object that describes a specific property of an object, including its value and configuration attributes.
Syntax
Object.getOwnPropertyDescriptor(obj, prop)
Parameters
obj ? The object whose property descriptor you want to retrieve.
prop ? The name or Symbol of the property whose descriptor you want to retrieve.
This method returns a property descriptor object if the property exists, or undefined if it doesn't.
Property Descriptor Attributes
A property descriptor contains the following attributes:
value ? The value associated with the property.
writable ?
trueif the property value can be changed,falseotherwise.enumerable ?
trueif the property shows up during enumeration (for...in loops),falseotherwise.configurable ?
trueif the property can be deleted or its attributes can be changed,falseotherwise.
Example 1: Basic Usage
<!DOCTYPE html>
<html lang="en">
<head>
<title>Property Descriptors</title>
</head>
<body>
<h1 style="color: green;">
Welcome To Tutorials Point
</h1>
<script>
// Create an object
const obj = {
property1: "Tutorials Point",
property2: "Simply Easy Learning"
};
// Get property descriptors
const descriptor1 = Object.getOwnPropertyDescriptor(obj, 'property1');
const descriptor2 = Object.getOwnPropertyDescriptor(obj, 'property2');
console.log("property1 configurable:", descriptor1.configurable);
console.log("property1 enumerable:", descriptor1.enumerable);
console.log("property1 value:", descriptor1.value);
console.log("property1 writable:", descriptor1.writable);
console.log("property2 value:", descriptor2.value);
</script>
</body>
</html>
property1 configurable: true property1 enumerable: true property1 value: Tutorials Point property1 writable: true property2 value: Simply Easy Learning
Example 2: Complete Descriptor Objects
<!DOCTYPE html>
<html lang="en">
<head>
<title>Property Descriptors</title>
</head>
<body>
<h1 style="color: green;">
Welcome To Tutorials Point
</h1>
<script>
// Create an object
const obj = {
name: "JavaScript",
version: "ES2024"
};
// Get complete descriptor objects
const nameDescriptor = Object.getOwnPropertyDescriptor(obj, 'name');
const versionDescriptor = Object.getOwnPropertyDescriptor(obj, 'version');
console.log("Name descriptor:", nameDescriptor);
console.log("Version descriptor:", versionDescriptor);
// Check for non-existent property
const nonExistent = Object.getOwnPropertyDescriptor(obj, 'nonExistent');
console.log("Non-existent property:", nonExistent);
</script>
</body>
</html>
Name descriptor: {value: 'JavaScript', writable: true, enumerable: true, configurable: true}
Version descriptor: {value: 'ES2024', writable: true, enumerable: true, configurable: true}
Non-existent property: undefined
Example 3: Using Object.getOwnPropertyDescriptors()
To get all property descriptors of an object at once, use Object.getOwnPropertyDescriptors():
<!DOCTYPE html>
<html lang="en">
<head>
<title>All Property Descriptors</title>
</head>
<body>
<h1 style="color: green;">
Welcome To Tutorials Point
</h1>
<script>
const obj = {
name: "Tutorial",
type: "JavaScript",
difficulty: "Beginner"
};
// Get all property descriptors
const allDescriptors = Object.getOwnPropertyDescriptors(obj);
console.log("All descriptors:", allDescriptors);
// Access individual descriptors
console.log("Name descriptor:", allDescriptors.name);
console.log("Type descriptor:", allDescriptors.type);
</script>
</body>
</html>
All descriptors: {name: {value: 'Tutorial', writable: true, enumerable: true, configurable: true}, type: {value: 'JavaScript', writable: true, enumerable: true, configurable: true}, difficulty: {value: 'Beginner', writable: true, enumerable: true, configurable: true}}
Name descriptor: {value: 'Tutorial', writable: true, enumerable: true, configurable: true}
Type descriptor: {value: 'JavaScript', writable: true, enumerable: true, configurable: true}
Common Use Cases
Object Cloning ? Creating exact copies of objects with the same property characteristics
Property Validation ? Checking if properties can be modified before attempting changes
Debugging ? Understanding why certain properties behave unexpectedly
Metaprogramming ? Building libraries that need to inspect object structures
Conclusion
Property descriptors provide detailed information about object properties, including their values and configuration attributes. Use Object.getOwnPropertyDescriptor() for individual properties or Object.getOwnPropertyDescriptors() for all properties at once.
