[英]Don't really understand Boolean() function
Is these two are functionally equal?If so,how(important)? 这两个在功能上是否相等?如果是,如何(重要)?
arr.filter(function(val){
return Boolean(val)
});
arr.filter(Boolean);
Also,are these two are functionally equal? 而且,这两个在功能上是否相等?
var x = new Boolean(expression);
var x = Boolean(expression);
The second one no, the one using new
creates an instance of Boolean, while the other just returns the boolean equivalent of what was passed to it, eg turn a truthy/falsy value into an actual boolean true/false value. 第二个否定,一个使用
new
创建一个Boolean实例,而另一个仅返回传递给它的值的布尔值等效项,例如将一个true / falsy值转换为实际的布尔值true / false。
console.log( "Is instance: ", (new Boolean(true)) instanceof Boolean ); console.log( "Is not instance: ", Boolean(true) instanceof Boolean ); console.log("truthy to bool: ", Boolean(-1) ); console.log("falsey to bool: ", Boolean(0) ); console.log("falsey to bool: ", Boolean("") ); console.log("truthy to bool: ", Boolean("test") );
They are also different in how you would use them in a conditional statement. 它们在条件语句中的用法上也有所不同。 For instance a primitive boolean value you can use alone in a conditional
例如,您可以在条件条件中单独使用原始布尔值
if(true) //
if(false) //
But if you try to test with a Boolean instance alone, the conditional will always be true
as it is an object, and all objects are truthy, even empty objects 但是,如果尝试单独使用布尔型实例进行测试,则条件将始终为
true
因为它是一个对象,并且所有对象都是真实的,即使是空对象
var d = new Boolean(false);
if(d){
console.log("Gets called even though the Boolean instance holds a false value");
}
The first is equivalent, in that they result in the same thing, because in both cases a boolean value will be returned to the filter
internals. 第一个是等效的,因为它们会导致相同的结果,因为在两种情况下,布尔值都将返回到
filter
内部。
arr.filter(Boolean);
Boolean
here is used as a callback method and takes the return value directly from Boolean's returned value. 此处的
Boolean
用作回调方法,并且直接从布尔值的返回值中获取返回值。
While doing 在做的时候
arr.filter(function(val){
return Boolean(val)
});
is using an anonymous function as the callback method and so the value is indirectly returned from Boolean
. 使用匿名函数作为回调方法,因此该值从
Boolean
间接返回。 But in both cases a value is passed to Boolean
and then returned so the result is the same. 但是在两种情况下,都将值传递给
Boolean
,然后返回,因此结果是相同的。
Note though the below would not be the same: 请注意,尽管以下内容可能不同:
arr.filter(function(val){
return new Boolean(val);
});
As stated above, an instance of Boolean is an object, it will always be a truthy value. 如上所述,布尔型的实例是一个对象,它将始终是真实值。 So in the above example all elements of the array would pass the test and therefore you would end up with same array that you were trying to filter.
因此,在上面的示例中,数组的所有元素都将通过测试,因此最终将得到您尝试过滤的数组。
The use of the anonymous function lets you do other things before ultimately processing/returning val
as a boolean, eg manipulate val
before passing it to Boolean
使用匿名函数可以让您在最终将
val
作为布尔值处理/返回之前进行其他操作,例如,在将val
传递给Boolean
之前对其进行操作
First snippet, yes these are functionally identical. 第一个摘要, 是的,它们在功能上是相同的。
Second version, no these will result in different values. 第二个版本, 没有这些将导致不同的值。 The first will result in a
Boolean
object instance with a truthy/falsey value and the second will result in a primitive truthy/falsey value. 第一个将导致
Boolean
对象实例具有true / falsey值,第二个将导致原始的 true / falsey值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.