Javascript-Getters
The getter return value is used when a property is being accessed.
Syntax:
{get prop() { ... } }
{get [expression]() { ... } }
Parameters:
prop: Name of the property to bind to the given function.
expression: Starting with ECMAScript 2015, and use expressions for a computed property name to bind to the given function().
Example:
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript i2tutorials
</title>
</head>
<body style="text-align:center;">
<div style="background-color: yellow;">
<h1>
Welcome to I2tutorials!.
</h1>
<h2>
JavaScript I2tutorials
</h2>
<p id="i2"></p>
</div>
<script>
// Create an object:
var person = {
name: "i2tutorials",
get lang() {
return this.name;
}
};
// Display data from the object using a getter
document.getElementById("i2").innerHTML
= person.name;
</script>
</body>
</html>
OUTPUT:
