ECMAScript 6 getter

We use the get syntax binds an object property to a function that will be called when that property is looked up. Here is how to use it…

(function() {
var obj = {
  get hello() {
    return "hello world!"

alert(obj.hello); //hello world!

Defining a getter on new objects in object initializers

Sometimes it is desirable to allow access to a property that returns a dynamically computed value, or you may want to reflect the status of an internal variable without requiring the use of explicit method calls. In JavaScript, this can be accomplished with the use of a getter. It is not possible to simultaneously have a getter bound to a property and have that property actually hold a value, although it is possible to use a getter and a setter in conjunction to create a type of pseudo-property.

Like this post? Kindly share!