javascript 吸气剂和二传手

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 吸气剂和二传手相关的知识,希望对你有一定的参考价值。

// When the property is accessed, the return value from the getter is used. 
// When a value is set, the setter is called and passed the value that was set. 
// It’s up to you what you do with that value, but what is returned from the setter is the value that was passed in – so you don’t need to return anything.
// Getter functions are meant to simply return (get) the value of an object's private variable to the user without the user directly accessing the private variable.

class Animal {
  constructor(species, color) {
    this.species = species,
    this.color = color
  }
  static info() {
    console.log('Here is the info');
  }
  get description() {
    console.log(`This is a description of the ${this.species}`);
  }
  set newColor(value) {
    this.color = value; 
  }
  get newColor() {
    return this.color;
  }
}
const bear = new Animal('bear', 'brown');
bear.newColor = 'red'; // Set the color to a new color. then calls get function.

以上是关于javascript 吸气剂和二传手的主要内容,如果未能解决你的问题,请参考以下文章

Thymeleaf 无法识别 Lombok 吸气剂和二传手

javascript 实现内置的吸气和二传手,控制私有对象属性的访问

物业获取者和二传手

javascript 智能二传手

测试 VueX 商店

为什么要创建一个比将变量声明为字段更好的方法?