Multiple Properties

Description

Object.defineProperties() method allows you to define multiple properties at once.

Example


var book = {};
// w ww. j a  v a2 s.c  o m
Object.defineProperties(book, {
     _year: {
         value: 2004
     },

     edition: {
         value: 1
   },

   year: {
       get: function(){
           return this._year;
       },

       set: function(newValue){
           if (newValue > 2004) {
               this._year = newValue;
               this.edition += newValue - 2004;
           }
       }
   }
});
book.year = 2005;
console.log(book.year);  
console.log(book.edition);  

book.year = 2000;
console.log(book.year);  
console.log(book.edition);  

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions