set special operatorS2C Home « Operators « set

Property binding.

Description

The set special operator allows us to bind an object property to a function. When that property is accessed the function will be called.

Syntax


Signature Description
{set propName(varName) { . . . } }special operator allows us to bind an object property to a function.
  • When that property is accessed the function will be called.

Parameters


Parameter Description
propNameThe property name to bind to the function.
varNamea variable label to hold the value assigned to propName.

Examples

Following is an example of the set special operator which adds to an array each time we create a new HomeOwner object.


// The set operator.
function HomeOwner(firstName,lastName) {  
  this.firstName=firstName;  
  this.lastName=lastName;  

  aList[aCount] = {  
    set incCount(owner) {
      return owner;  
    },
    owner: this.firstName + ' ' + this.lastName
  }  
}  
var aList = new Array();
var aCount = 0;
owner = new HomeOwner('Barney','Magrew'); 
aCount++;
owner = new HomeOwner('Manny','Simon'); 
aCount++;
owner = new HomeOwner('Arnie','Padrew'); 
var b=JSON.stringify(aList);  
alert('The list of objects we created with the set operator: ' + b);  

Press the button below to action the above code:


Related Tutorials

JavaScript Basic Tutorials - Lesson 7 - Objects

go to home page Homepage go to top of page Top