[英]Set truthy value of JavaScript custom Object
Is there a property from an object that I can modify to specify the truth value for the object. 是否存在来自对象的属性,我可以修改该属性以指定对象的真值。 For example add a line inside:
例如,在里面添加一行:
var myObj{...}
so that I can determine whether the code block associated with the following if-statement is executed: 这样我就可以确定是否执行了与以下if语句关联的代码块:
if(myObj){
//dosomething
}
Something analogous in Python would be overriding the __bool__
method. Python中类似的东西会覆盖
__bool__
方法。
JavaScript does not have a mechanism to override operations like this. JavaScript没有覆盖这样的操作的机制。 Objects will always be truthy as defined by the ECMAScript spec pointed out by Felix Kling.
根据Felix Kling指出的ECMAScript规范的定义,对象将始终是真实的。 The closest that I can think of that does something similar to that is the
toString()
method; 我能想到的最接近的东西是
toString()
方法; but there is no toBool()
method that you can override. 但是没有可以覆盖的
toBool()
方法。
I'm not super familiar with Python, but I'm pretty sure that __bool__
is basically a way to override how an Object is cast to a Boolean. 我对Python并不是很熟悉,但我很确定
__bool__
基本上是一种覆盖Object如何转换为布尔值的方法。 I'm comparing that to similar language features like __lt__
which I would also assume is how Python allows you to override certain operators in similar to C++. 我将它与类似的语言特性(如
__lt__
进行比较,我还假设Python允许您覆盖类似于C ++的某些运算符。
It's not directly related, but you should also consider reading this question: Overloading Arithmetic Operators in JavaScript? 它没有直接关系,但您还应该考虑阅读这个问题: 在JavaScript中重载算术运算符? .
。
Consider also this excerpt from 2ality's Why all objects are truthy in JavaScript , emphasis mine: 还要考虑2ality中的这段摘录为什么JavaScript中的所有对象都是真实的 ,强调我的:
The conversion to boolean is different for historic reasons: For ECMAScript 1, it was decided to not allow objects to configure that conversion .
由于历史原因,转换为布尔值是不同的:对于ECMAScript 1, 决定不允许对象配置该转换 。 The rationale was as follows.
理由如下。 The boolean operators ||
布尔运算符|| and && preserve the values of their operands.
和&&保留其操作数的值。 Therefore, the same object may have to be coerced several times.
因此,可能必须多次强制相同的对象。 That was considered a performance problem and led to the rejection.
这被认为是一个性能问题并导致拒绝。
When you say if(myObj){...}
in javascript. 当你在javascript中说
if(myObj){...}
时。 The condition will be true if myObj
value is not one of undefined
, null
, 0
, false
, zero length string
values. 如果
myObj
值不是undefined
, null
, 0
, false
, zero length string
值之一,则该条件为true。 so any value except this values makes the condition true
in javascript. 所以除了这个值之外的任何值都使得条件在javascript中为
true
。
That doesn't exist in JavaScript, if you're defining a map then it doesn't make sense to have it behave as a boolean
value. 这在JavaScript中不存在,如果您正在定义一个映射,那么将它表现为
boolean
值是没有意义的。
For that you can either set a key inside that object to be a boolean
: 为此,您可以将该对象内的键设置为
boolean
:
let myObj = { isValid: true }
if (myObj.isValid) {
// do something
}
or assign null
to it in which case you can check for it's validity: 或者为它指定
null
,在这种情况下你可以检查它的有效性:
let myObj = null
if (myObj === null) {
// do something
}
Also, it would be better practice to check the object against a real value instead of relying on the magic of truthy values as it may result in false result. 此外,更好的做法是根据实际值检查对象,而不是依赖于真实值的魔力,因为它可能导致错误的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.