Java Reference
In-Depth Information
You can also access the property that is a function using the bracket notation. The
syntax looks a bit awkward. This function call that uses the dot notation can be replaced
with the bracket notation, as shown:
var fullName = john["getFullName"]();
print("Full name is " + fullName);
Full name is John Jacobs
Defining Accessor Properties
An accessor property is also called getter and setter methods. You can think of an
accessor property as a set of getXxx() and setXxx() methods in a Java class. When
the xxx property is read, the getter method with the xxx name is called; when the xxx
property is set, the setter method with the xxx name is called. The getter method declares
no formal parameters. The setter method declares one formal parameter. A property can
have only a getter method, only a setter method, or both. The following is the syntax for
defining the accessor property:
{ prop1: value1, /* A data property */
prop2: value2, /* A data property */
/* The getter for the property propName */
get propName() {
// Getter method's body goes here
},
/* The setter for the property propName */
set propName(propValue) {
// Setter method's body goes here
}
}
Defining an accessor property is the same as declaring functions with the keyword
function replaced with the keywords get and set . The keywords get and set are used to
define the getter and setter methods, respectively. Notice that you do not use a colon to
define the accessor property, but you still need to use a comma to separate two properties
of the object. The following code defines an object with two data properties named fName
and lName , and an accessor property named fullName :
// A person object with fName and lName as data proeprties
// and fullName as an accessor property
var john = {fName: "John",
lName: "Jacobs",
get fullName() {
 
Search WWH ::




Custom Search