What is the “get” keyword before a function in a class?

前端 未结 4 1021
执念已碎
执念已碎 2020-12-13 03:02

What does get mean in this ES6 class? How do I reference this function? How should I use it?

class Polygon {
  constructor(height, width) {
           


        
4条回答
  •  佛祖请我去吃肉
    2020-12-13 03:37

    or more simple way it just call the function without needing to user "()" just by typing the function name

    the two above function are equal attention to person.fullName() and person.fullName

    const person = {
        firstName: 'Willem',
        lastName: 'Veen',
        fullName() {
            return `${this.firstName} ${this.lastName}`;
        }
    
    }
    
    console.log(person.fullName());

    const person = {
        firstName: 'Willem',
        lastName: 'Veen',
        get fullName() {
            return `${this.firstName} ${this.lastName}`;
        }
    
    }
    
    console.log(person.fullName);

提交回复
热议问题