The defineProperty() and defineProperties() are methods of the JavaScript Object class that can be used to add and modify properties dinamicaly directly on an object.
They are supported by: Mozilla Firefox 4+, Google Chrome, Internet Explorer 9+, Opera 11+, Safari 5+.


defineProperty() - Defines a new property, or modifies an existing property on an object, and returns the object.
Object.defineProperty(obj, prop, {
  value: Prop_Value,
  configurable: true,
  enumerable: true
- obj - The object on which to define the property.
- prop - The name of the property to be defined or modified.
- Prop_Value - The value of the property specifieed to "prop" parameter.
- configurable - if this attribute is set to false, the new added property cannot be then modified. Defaults to false.
- enumerable - if this attribute is set to false, the property not show up during enumeration of the properties on the corresponding object, will be stored only in memory. Defaults to false.

The third argument of this method (with the value of the property) is more complex, with some additional attributes. For a complete documentation see this page:

Example (see the comments in code):
<script type="text/javascript">
// define an object
var sites = {
  'CoursesWeb': ''

// modifies the "CoursesWeb" property
Object.defineProperty(sites, 'CoursesWeb', {
  value: ''

// adds a new property
Object.defineProperty(sites, 'MarPlo', {
  value: '',
  configurable: true,
  enumerable: true

// Test
alert(sites.CoursesWeb +'\n'+ sites.MarPlo);

/* Result:


defineProperties() - Defines new or modifies multiple existing properties same time directly on an object, returning the object.
Object.defineProperties(obj, {
  'prop1': { value: value_prop1 },
  'prop2': { value_prop2 },
  // ...
- obj - The object on which to define or modify properties.
- 'prop1', 'prop2' - The name of the properties to be defined or modified.
- value_prop1, value_prop2 - The values of the properties to be defined or modified.

Example (see the comments in code):
<script type="text/javascript">
// define an object
var sites = {
  'CoursesWeb': ''

// modifies the "CoursesWeb" property, and adds two new properties
// stores the object in another variable
var sites2 = Object.defineProperties(sites, {
  CoursesWeb: { value: '' },
  MarPlo: { value: '' },
  id: { value: 8 }

// Test properties in "sites" and "sites2"
alert(sites.CoursesWeb +'\n'+;

/* Result:

• Here's a practical example of using of the defineProperty() method. Data selected by the user from Select dropdown list are added as properties into an object, then, the object is displayed as JSON string.
<div id="showob">JSON object</div>
<form action="#" method="post">
 Name: <input type="text" name="nmusr" id="nmusr" onkeyup="setObj(, this.value)" /><br/>
 Age: <select name="agusr" id="agusr" onchange="setObj(, this.value)">
  <option value="5-8">5-8</option>
  <option value="9-14">9-14</option>
  <option value="15-20">15-20</option>
  <option value="21-30">21-30</option>
  <option value="31-45">31-45</option>
 Gender: <select name="gdusr" id="gdusr" onchange="setObj(, this.value)">
  <option value="male">Male</option>
  <option value="female">Female</option>
<script type="text/javascript">
// set "usrob" with one property
var usrob = {nmusr: 'name'};

// this function adds /modifies property in usrob, using defineProperty()
// converts it into a string, and displays it
function setObj(prop, val) {
  Object.defineProperty(usrob, prop, {
    value: val,
    configurable: true,
    enumerable: true
  document.getElementById('showob').innerHTML = 'JSON object: <strong>'+ JSON.stringify(usrob) +'</strong>';  
Demo. Write a name in the text field, select an Age and Gender.
JSON object

Daily Test with Code Example

Which tag is used in <table> to create table header cell?
<thead> <th> <td>
  <th>Title 1</th>
  <th>Title 2</th>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){"");
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
defineProperty and defineProperties

Last accessed pages

  1. Add Pause in JavaScript script (7964)
  2. Read Excel file data in PHP - PhpExcelReader (50840)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (3083)
  4. Extract / Unzip ZIP archive files with PHP (1732)
  5. SHA512 Encrypt hash in JavaScript (8211)

Popular pages this month

  1. Qwop (2996)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (2371)
  3. Get the value of the selected /checked checkboxes in a form (1497)
  4. Insert, Select and Update NULL value in MySQL (1339)
  5. Add, Change, and Remove Attributes with jQuery (1225)