简体   繁体   English

如何创建可根据其他一些值自动更新的属性?

[英]How do I make a property that updates itself automatically based on some other value(s)?

Suppose I have an object like this with some arbitrary values... 假设我有一个带有一些任意值的对象...

let object = {
  value: 5,
  min: 1,
  max: 100,
  valid: true
}

How could I make it so that the valid property updates automatically if the value property is not within the range? 如果value属性不在范围内,如何使valid属性自动更新? I know I could have made a getter in the object like this... 我知道我可以在这样的物体上getter ...

get valid() {
  if (this.value > this.max || this.value < this.min) {
    return false
  else {
    return true
  }
}

...but this only updates valid when it is accessed. ...但这仅在访问时更新valid I need valid to always be up to date based on the other values of the object. 我需要valid永远是最新的基于对象的其他值。

It's often that I eventually realize that my entire way of thinking about a problem was wrong. 通常,我最终意识到我对问题的整个思考方式是错误的。 Is this one of those cases? 这是其中一种吗? For example, I know that if I use setter functions for updating all three of the other objects I could use them to keep valid up to date, but that seems like the wrong approach. 例如,我知道如果我使用setter函数来更新所有其他三个对象,则可以使用它们来保持valid最新状态,但这似乎是错误的方法。

What should I do? 我该怎么办?

This is the only way I can think of doing it without getters. 这是我可以想到的没有吸气剂的唯一方法。 It updates the value of valid every time the other properties are updated. 每当其他属性更新时,它都会更新valid

 var obj = { val: 5, minimum: 1, maximum: 100, valid: true, set value(v) { this.val = v; this.updateValid(); }, set min(m) { this.minimum = m; this.updateValid(); }, set max(m) { this.maximum = m; this.updateValid(); }, updateValid : function() { this.valid = (this.val >= this.minimum && this.val <= this.maximum) } } obj.value = -1; console.log(obj); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使随机值不重复? (JavaScript) - How do I make a random value not repeat itself? (JavaScript) 使用 Angular 的 matAutocomplete,如何显示对象的属性,但在别处引用对象本身? - Using Angular's matAutocomplete, how do I display an object's property, but reference the object itself elsewhere? 如何通过对象的属性值对对象数组进行排序,该属性值本身不是自然可比的,但具有基于规则的优先级? - How does one sort an array of objects by an object's property value which itself is not naturally comparable but has a rule based precedence? 如何根据属性值对数组进行分组? - How do I group an array based on a property value? 如何使我的 object 属性值动态化? - How do I make my object property value dynamic? 如何将输入值更改为ID? - How do I make an input change it's value to it's id? 如何根据其他属性对对象的某些属性进行分组 - How to group some properties of objects based on other property 根据对象的属性值(相对于其他元素中发生的次数)过滤数组 - Filter array based on an object's property value relative to how many times it occurs in other elements 如果两个选择框相互匹配,如何使它们相互关闭并自动选择不匹配的第二个选项? - How do I make 2 select boxes to dismiss each other if they mach and automatically select the second option to not match? 如何使 API 响应在 angular 中自动更新 - How to make API response updates automatically in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM