get special operatorS2C Home « Operators « get

Property binding.

Description

The get 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
{get propName() { . . . } }Special operator which allows us to bind an object property to a function.

Parameters

Parameter Description
propNameThe property name to bind to the function.

Examples

Following is an examples of the get special operator.


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

  aName = {  
    get fullname() {  
      return this.name;  
    },  
    name: this.firstName + ' ' + this.lastName 
  }  
}  
var aName;
owner = new HomeOwner('Barney','Magrew'); 
alert(aName.fullname);
owner = new HomeOwner('Manny','Simon'); 
alert(aName.fullname);  

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