Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js object getter

const 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"
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #js #object #getter
ADD COMMENT
Topic
Name
3+5 =