What are getters and setters for in ECMAScript 6 classes?

前端 未结 4 2013
太阳男子
太阳男子 2020-11-29 17:44

I am confused as to what the point of getters and setters are in ECMAScript 6 classes. What is the purpose? Below is an example I am referring to:

class Empl         


        
4条回答
  •  长情又很酷
    2020-11-29 18:07

    These setter and getter allow you to use the properties directly (without using the parenthesis)

    var emp = new Employee("TruMan1");
    
    if (emp.name) { 
      // uses the get method in the background
    }
    
    emp.name = "New name"; // uses the setter in the background
    

    This is only to set and get the value of the property.

提交回复
热议问题