Sunday, 9 December 2018

Getter Syntax

New Getters



var obj = {

  log: ['a', 'b', 'c'],

  get latest() {

    if (this.log.length == 0) {

      return undefined;

    }

    return this.log[this.log.length - 1];

  }

}

 

console.log(obj.latest);

// expected output: "c"

-----------------------------otherway

 

var obj = {

  log: ['a', 'b', 'c'],

  latest() {

    if (this.log.length == 0) {

      return undefined;

    }

    return this.log[this.log.length - 1];

  }

}

console.log(obj.latest());

// expected output: "c"

 

 



Sources
Formal Javascript Documentation

No comments:

Post a comment