The Wayback Machine - https://web.archive.org/web/20250207102824/https://www.geeksforgeeks.org/javascript-object-defineproperty-method/
Open In App

JavaScript Object defineProperty() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The Object.defineProperty() method in JavaScript is a Standard built-in object which defines a new property directly on an object or it can also modify the existing property of an object and return the object. 

Syntax:

Object.defineProperty(obj, prop, descriptor)

Parameters:

This method accepts three parameters as mentioned above and described below:

  • Obj: This parameter holds the Object on which the user is going to define the property.
  • Prop: This parameter holds the name of a property that is going to be defined or modified.
  • Descriptor: This parameter holds the descriptor for the property being defined or modified.

Return Value:

  • This method returns the object which is passed as the argument to the function. 

Example 1: In this example, we will add new properties to an object and print it in the console using the Object.defineProperty() method in JavaScript.

javascript
const geek1 = {};
Object.defineProperty(geek1, 'prop1', {
    value: 65,
    writable: false
});
geek1.prop1 = 7;
console.log(geek1.prop1);

const geek2 = {};
Object.defineProperty(geek2, 'prop2', {

    value: 54,
    value: 23,
    value: 12 * 9,
});
geek2.prop2;
console.log(geek2.prop2);

Output:

65
108

Example 2: In this example, we will add new properties to an object and print it in the console using the Object.defineProperty() method in JavaScript.

javascript
function gfg() {
}

let result;
Object.defineProperty(gfg.prototype, "valx", {
    get() {
        return result;
    },
    set(valx) {
        result = valx;
    }
});

let vala = new gfg();
let valb = new gfg();
vala.valx = 6;
console.log(valb.valx);

function gfg1() {
}

gfg1.prototype.valx = 4;
Object.defineProperty(gfg1.prototype, "valy", {
    writable: false,
    value: 8
});

let vala = new gfg1();
vala.valx = 4;
console.log(vala.valx);
console.log(gfg1.prototype.valx);
vala.valy = 2;
console.log(vala.valy);
console.log(gfg1.prototype.valy);

Output:

6
4
4
8
8

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers:

The browsers supported by Object.defineProperty() method are listed below:

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Edge

JavaScript Object defineProperty() Method- FAQs

What does the Object.defineProperty() method do in JavaScript?

The Object.defineProperty() method defines a new property directly on an object or modifies an existing property on an object, and returns the object.

What is the purpose of using Object.defineProperty()?

The main purpose is to define or modify properties with precise control over their attributes, such as making properties read-only, non-enumerable, or implementing custom getter and setter functions.

How does Object.defineProperty() interact with strict mode?

In strict mode, attempts to write to a non-writable property, delete a non-configurable property, or redefine a non-configurable property will throw an error. In non-strict mode, these operations fail silently.

How does Object.defineProperty() handle existing properties?

If the property already exists, Object.defineProperty() modifies the property’s attributes as specified. If the property does not exist, it is created with the specified attributes.

What is the most common use of the Object.defineProperty() method?

  • Creating properties with custom getters and setters
  • Making properties read-only or non-enumerable
  • Controlling property attributes precisely
  • Implementing data encapsulation and access control in objects


Next Article

Similar Reads

three90RightbarBannerImg