以下代码段中运算符“&&”的详细信息是什么?
what the operator " &&" do in detail in the following snippet?
我想知道为什么属性的值在散列对象中不是“true”,我在语句“hash.jason =”中将属性赋值为“true”真“。
var array=[]
var array=[]
hash.jason = true && array.push('jason')
hash.tom = true && array.push('tom')
hash.lucy = true && array.push('lucy')
the output is:
array
(3) ["jason", "tom", "lucy"]
hash
{jason: 1, tom: 2, lucy: 3}
&& 是逻辑与。
接下来, array.push
returns 更新数组的长度,因此您将其分配给散列。但是,没有 true &&
你也会得到同样的结果
const array = [];
const hash = {};
// array.push itself returns length of
// updated array, so here you
// assign this length to hash
hash.jason = true && array.push('jason');
// But will not push to array if false
hash.tom = false && array.push('tom');
// Without 'true &&' will do the same
hash.lucy = array.push('lucy');
// Log
console.log(array)
console.log(hash)
你问题的第二部分 - 为什么它分配的不是 true
而是 number
,请参阅下面的小代码...逻辑 AND 表达式从左到右求值,测试可能的“ short-circuit" 使用以下规则进行评估:(some falsy expression) && expr
console.log(true && 13);
console.log(false && 99);
我想知道为什么属性的值在散列对象中不是“true”,我在语句“hash.jason =”中将属性赋值为“true”真“。
var array=[]
var array=[]
hash.jason = true && array.push('jason')
hash.tom = true && array.push('tom')
hash.lucy = true && array.push('lucy')
the output is:
array
(3) ["jason", "tom", "lucy"]
hash
{jason: 1, tom: 2, lucy: 3}
&& 是逻辑与。
接下来, array.push
returns 更新数组的长度,因此您将其分配给散列。但是,没有 true &&
const array = [];
const hash = {};
// array.push itself returns length of
// updated array, so here you
// assign this length to hash
hash.jason = true && array.push('jason');
// But will not push to array if false
hash.tom = false && array.push('tom');
// Without 'true &&' will do the same
hash.lucy = array.push('lucy');
// Log
console.log(array)
console.log(hash)
你问题的第二部分 - 为什么它分配的不是 true
而是 number
,请参阅下面的小代码...逻辑 AND 表达式从左到右求值,测试可能的“ short-circuit" 使用以下规则进行评估:(some falsy expression) && expr
console.log(true && 13);
console.log(false && 99);